更改路由时AngularJS UI路由器事件
使用ngRoute once可以在以下事件中连接:更改路由时AngularJS UI路由器事件,angularjs,angularjs-routing,Angularjs,Angularjs Routing,使用ngRoute once可以在以下事件中连接:$routeChangeStart,并执行不同的操作 app.run(function ($rootScope, $location) { $rootScope.$on("$routeChangeStart", function (event, next, current) { ................ 使用UI路由器可以实现同样的效果吗?是的,可以: $rootScope.$on("$stateChangeStart
$routeChangeStart
,并执行不同的操作
app.run(function ($rootScope, $location) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
................
使用UI路由器可以实现同样的效果吗?是的,可以:
$rootScope.$on("$stateChangeStart",
function (event, toState, toParams, fromState, fromParams) {
或者只是使用
$scope.$on("$stateChangeStart",...);
如果希望在单个页面上触发此问题。请检查此处的答案,这是解决此问题的正确方法。删除所有侦听器可能会产生未知的效果,因为可能会在其他位置添加侦听器。您需要删除您添加的一个,而不是全部 检查此问题: 在此处复制代码以确保完整性:
animateApp.controller('mainController', function($scope, $rootScope, service) {
$scope.pageClass = 'page-home';
var unregister = $rootScope.$on('service.abc', function (newval) {
console.log($scope.$id);
});
$scope.$on('$destroy', unregister);
});
是否每次路线更改时都会调用此命令?或者在应用程序第一次运行的任何时候?@Notflip每次状态更改之前我都必须使用
$statechangesucture
,因为$stateChangeStart
上的东西还没有准备好,但谢谢它让我走到了需要的地方。