Angularjs 使用角组件路由器时,如何检测组件变化?
现在我正在使用ui路由器来处理授权:Angularjs 使用角组件路由器时,如何检测组件变化?,angularjs,angular-routing,Angularjs,Angular Routing,现在我正在使用ui路由器来处理授权: $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {} 但是,自从Angular1.5以来,我如何使用ngComponentRouter来实现这一点呢?在您拥有的新路由器中: angular.module('app', []) .controller('MyController', ['user', '$http'
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {}
但是,自从Angular1.5以来,我如何使用ngComponentRouter来实现这一点呢?在您拥有的新路由器中:
angular.module('app', [])
.controller('MyController', ['user', '$http', MyController]);
function MyController(user, $http) {
this.user = user;
this.$http = $http;
}
// Before switching to a new component, this hook runs for
// each active component in the app. If any of them
// return false, a rejected promise, or a promise that resolves to false,
// the navigation is cancelled.
MyController.prototype.canActivate = function() {
return this.user.isAdmin;
};
//This hook fires just before the nagivation finishes.
MyController.prototype.activate = function() {
return this.bigFiles = this.$http.downloadBigFiles();
};
当用户尚未登录时,如何将其重定向到“/login”页面?使用canActivate钩子,但如何?没有像$stateChangeStart事件那样的“$componentChangeStart”可供侦听您拥有canActivate挂钩,在您移动到此组件之前,angular将触发此函数。它与stateChangeStart相同。