Angularjs 为什么$routeChangeSuccess从未被调用?
我正在我的应用程序上做类似于下面的事情,但我只是无法获得routeChangeSuccess事件Angularjs 为什么$routeChangeSuccess从未被调用?,angularjs,angular-ui,angular-ui-router,Angularjs,Angular Ui,Angular Ui Router,我正在我的应用程序上做类似于下面的事情,但我只是无法获得routeChangeSuccess事件 var myapp = angular.module('myapp', ["ui.router", "ngRoute"]); myapp.controller("home.RootController", function($rootScope, $scope, $location, $route) { $scope.menus = [{ 'name': 'Home ', 'link'
var myapp = angular.module('myapp', ["ui.router", "ngRoute"]);
myapp.controller("home.RootController",
function($rootScope, $scope, $location, $route) {
$scope.menus = [{ 'name': 'Home ', 'link': '#/home'}, {'name': 'services', 'link': '#/services'} ]
$scope.$on('$routeChangeSuccess', function(event, current) {
alert('route changed');
});
}
);
myapp.config(
function($stateProvider, $urlRouterProvider, $routeProvider) {
$urlRouterProvider.otherwise("/home");
$stateProvider
.state('home', {
url: "/home",
//template: '<h1>Home Screen</h1>'
templateUrl: "/Client/Views/Home/Home.htm"
})
.state('services', {
url: "/services",
//template: '<h1>Service screen</h1>'
templateUrl: "/Client/Views/Home/service.htm"
});
});
var myapp=angular.module('myapp',[“ui.router”,“ngRoute]”);
myapp.controller(“home.RootController”,
函数($rootScope、$scope、$location、$route){
$scope.menus=[{'name':'Home','link':'#/Home'},{'name':'services','link':'#/services'}]
$scope.$on(“$routeChangeSuccess”,函数(事件,当前){
警报(“路线改变”);
});
}
);
myapp.config(
函数($stateProvider、$urlRouterProvider、$routeProvider){
$urlRouterProvider。否则(“/home”);
$stateProvider
.州(“家”{
网址:“/home”,
//模板:“主屏幕”
templateUrl:“/Client/Views/Home/Home.htm”
})
.州(“服务”{
url:“/services”,
//模板:“服务屏幕”
templateUrl:“/Client/Views/Home/service.htm”
});
});
下面一个非常简单的html也失败了
<body ng-controller="home.RootController">
<ul class="nav">
<li ng-repeat="menu in menus" "="">
<a href="{{menu.link}}">{{menu.name}}</a>
</li>
</ul>
<div ui-view> No data yet!</div>
</body>
请检查此wiki:。摘录:
- $stateChangeSuccess-状态转换完成后激发
$scope.$on(“$stateChangeSuccess”,
函数(事件、toState、TopParams、fromState、fromParams){…}
因此,不要使用
$routeChangeSuccess
而使用$stateChangeSuccess
要获取所有可用事件的详细信息,请查看wiki。在这里,您可以找到适合您的,可能是event$viewContentLoaded
..stateChange
事件现在已被弃用和删除,请改用
ui.router
不用于ngRoute
。请用一个或另一个。谢谢$viewContentLoaded就是我要找的。顺便说一句,我无法获取$viewContentLoading事件。不知道为什么。但是$viewContentLoaded的工作原理与此类似,请查看$stateChangeStart
。
$transitions.onSuccess({}, function () {
console.log("state changed");
});