小编xro*_*kam的帖子

Angular:来自服务的广播事件

我想以角度创建自定义事件.目标是该事件将由服务广播,并且必须在控制器中捕获.

我有我的服务:

function doSomething() {
        // do something...
        $rootScope.$broadcast("bced");
        $rootScope.$emit("emitted");
        console.log('event');
}
Run Code Online (Sandbox Code Playgroud)

和我的控制器:

$rootScope.$on("bced",function(){
    console.log("catched");
});
$rootScope.$on("emitted",function(){
    console.log("catched");
});

$scope.$on("bced",function(){
    console.log("catched");
});
$scope.$on("emitted",function(){
    console.log("catched");
});
Run Code Online (Sandbox Code Playgroud)

但我无法捕捉到控制器中的事件.显示console.log('event')但我无法捕获任何事件.有人可以解释一下如何做到这一点吗?$ rootScope在服务和控制器中正确声明.

service events controller angularjs

5
推荐指数
1
解决办法
6828
查看次数

标签 统计

angularjs ×1

controller ×1

events ×1

service ×1