Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS$routeParams:设置$scope变量_Angularjs_Angular Router - Fatal编程技术网

AngularJS$routeParams:设置$scope变量

AngularJS$routeParams:设置$scope变量,angularjs,angular-router,Angularjs,Angular Router,使用AngularJS 1.6.1,我有一个只有一个控制器的页面。我想使用$routeParams将$scope变量设置为(可选)路由参数的值。但不幸的是,这根本不起作用: var myApp = angular.module('myApp', ['ngRoute']); myApp.config(function($routeProvider) { $routeProvider .when('/', { templateUrl: '/partials/par

使用AngularJS 1.6.1,我有一个只有一个控制器的页面。我想使用$routeParams将$scope变量设置为(可选)路由参数的值。但不幸的是,这根本不起作用:

var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function($routeProvider) {
   $routeProvider
      .when('/', {
         templateUrl: '/partials/partial1.htm'
      })
      .when('/:artist/:album', {
         templateUrl: '/partials/partial1.htm'
      })
      .otherwise({
         redirectTo: '/'
      });
});

myApp.controller('myController', function($scope, $routeParams) {
   $scope.artist = $routeParams.artist;
   $scope.album = $routeParams.album;
});
使用console.log时,我可以看到正在设置值。。。但路由器似乎正在为参数化路由启动一个单独的控制器。因此,我尝试使用一个附加的控制器,然后使用$emit和$on将消息从第二个控制器传递到第一个控制器

var myApp = angular.module('myApp', ['ngRoute']);

myApp.config(function($routeProvider) {
   $routeProvider
      .when('/', {
         templateUrl: '/partials/partial1.htm'
      })
      .when('/:artist/:album', {
         templateUrl: '/partials/partial1.htm',
         controller: 'URLController'
      })
      .otherwise({
         redirectTo: '/'
      });
});

myApp.controller('URLController', function($scope, $routeParams) {
   $scope.$emit('UrlChanged', $routeParams.artist, $routeParams.album);
});

myApp.controller('myController', function($scope, $routeParams, $http) {
   $scope.$on('UrlChanged', function(event, artist, album) {
      $scope.artist = $routeParams.artist;
      $scope.album = $routeParams.album;
      // CORRECTION: It keeps firing events if I add this
      $scope.loadAlbum();
      // loadAlbum() contains a call to a Rest API on the same server via $http.get.
      // Obviously this triggers the creation of a new URLController????
   });
});
但这只会无限期地触发
UrlChanged
事件

更正:只有在我添加
$http.get
调用时,它才会一直启动

我必须做些什么才能实现我的目标?

尝试收听
$route
服务的事件(因为
MainController
似乎在实际事件/导航发生之前已初始化):


您可以尝试收听
$route
服务的事件,而不是在新控制器中发出您自己的事件?谢谢,就这样。我已经尝试$locationChangeSuccess。。。但那是个错误的地方
myApp.controller('MainController', ['$scope', function ($scope) {
    $scope.$on('$routeChangeStart', function ($event, next, current) {
        var params = next.params;
        if (params) {
            $scope.artist = params.artist;
            $scope.album = params.album;
        }
    });
}])