菜单位于<div>
之外通过 UI Router 的 ui-view
呈现的 block .
为了根据状态参数修改菜单,我需要从菜单 Controller 中访问当前状态参数,但是 $stateParams
在 ui-view
之外使用时变量是一个空对象部分。
我如何访问它们?
angular.module('myapp').controller('MenuCtrl', ['$scope', '$rootScope', '$stateParams', '$meteor', '$filter',
function($scope, $rootScope, $stateParams, $meteor, $filter) {
// ... $stateParams equals {}
}
]);
最佳答案
尝试观察 Controller 中的状态变化...
angular.module('myapp').controller('MenuCtrl', ['$scope', '$rootScope', '$meteor', '$filter',
function($scope, $rootScope, $meteor, $filter) {
//this watches for state changes
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
//do something when state changes
state = toState.name;
postid = toParams.postid;
console.log(toParams); //this is the stateParams you need
});
}
]);
关于angularjs - Angular UI 路由器 : Access state parameters outside ui-view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32142818/