gpt4 book ai didi

javascript - $location.path() 更改后在另一个 ng-view 中触发事件

转载 作者:行者123 更新时间:2023-11-29 22:07:48 24 4
gpt4 key购买 nike

我想在 $location.path() 更改 ng-view 后(不使用 $rootScope)在另一个 View 中触发警报。

在我的 Controller 触发事件​​中我有:

$location.path('/');
myService.setAlert("My alert");

我的服务传递“消息”并 $broadcast 一个事件

myService.broadcastAlert = function(){
$rootScope.$broadcast('alertChanged');
};

myService.setAlert = function(message){
myService.alert = message;
rootScope.$on('$routeChangeSuccess', function() {
myService.broadcastAlert();
});
}

我的其他 View 的 Controller 收到警报

$scope.$on('alertChanged', function(){
console.log("New Alert is triggered");
});

这里的问题是rootScope.$on('$routeChangeSuccess')之后继续运行

最佳答案

也许您需要注销:

myService.setAlert = function(message){
myService.alert = message;
var deregister = rootScope.$on('$routeChangeSuccess', function() {
myService.broadcastAlert();
deregister();
});
};

关于javascript - $location.path() 更改后在另一个 ng-view 中触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19975170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com