我使用它来创建我的网站,当我创建一个包含 $rootScope.on('$stateChangeStart') 的运行方法时,它不起作用。这是代码:
.run('Url',function($rootScope, $state, $location){
'ngInject' ;
console.log('Is working');
function message(to, toP, from, fromP) {
return from.name + angular.toJson(fromP) + " -> " + to.name + angular.toJson(toP);
}
$rootScope.$on("$stateChangeStart", function(evt, to, toP, from, fromP) {
console.log("Start: " + message(to, toP, from, fromP));
console.log('Not working');
});
当我加载网站时的第一个日志正在工作,但后两个日志不是,有什么问题吗?
如果您更改版本。
对于新的ui路由器(Angular 1.x版本)。您需要加载状态事件文件。
<script src="lib/angular-ui-router/release/stateEvents.min.js"></script>
在应用程序中添加加载它
angular.module('MyApp', ['ui.router', 'ui.router.state.events', ...
你可以使用 rootScope.$on 像
.run(['$rootScope', '$state', '$location',function($rootScope, $state, $location){
console.log('Is working');
function message(to, toP, from, fromP) {
return from.name + angular.toJson(fromP) + " -> " + to.name + angular.toJson(toP);
}
$rootScope.$on("$stateChangeStart", function(evt, to, toP, from, fromP) {
console.log("Start: " + message(to, toP, from, fromP));
console.log('Not working');
})
}]);
试试这个:"$viewContentLoading"或"$viewContentLoaded":
$rootScope.$on('$viewContentLoading', function() {
// your stuff here…
});
$rootScope.$on('$viewContentLoaded', function() {
// your stuff here…
});