2
我想在$ location.path()更改ng视图(不使用$ rootScope)之后触发另一个视图中的警报。
在我的控制器的触发事件,我有:
$location.path('/');
myService.setAlert("My alert");
我的服务传递 “信息” 和$播出
myService.broadcastAlert = function(){
$rootScope.$broadcast('alertChanged');
};
myService.setAlert = function(message){
myService.alert = message;
rootScope.$on('$routeChangeSuccess', function() {
myService.broadcastAlert();
});
}
我的另一种观点的控制器接收警报的事件
$scope.$on('alertChanged', function(){
console.log("New Alert is triggered");
});
这里的问题是rootScope。$ on('$ routeChangeSuccess')继续运行后
它完美,谢谢!但是我不太确定我是否理解它是如何工作的,你能告诉我在哪里可以阅读关于注销功能的更多信息吗? – lproust
当然:[1.1.5](http://code.angularjs.org/1.1.5/docs/api/ng.$rootScope.Scope)或[latest](http:// docs.angularjs.org/api/ng.$rootScope.Scope)。检查'$ on'方法的返回值。实际上这并不是什么大问题:'$ on()'返回一个注销函数。被调用时,它只是注销听众。 –
这仍然使用rootScope,不是吗? – mila