简体   繁体   English

Angular JS广播没有进入子范围

[英]Angular JS broadcast not getting inside child scope

Please refer - http://jsfiddle.net/U3pVM/18701/ 请参考-http://jsfiddle.net/U3pVM/18701/

In the console, I only see "outer1" being logged, the inner scope does not log. 在控制台中,我只看到“ outer1”正在记录,内部作用域未记录。 Is something missing? 缺少什么吗?

var app = angular.module("myapp", []);

app.controller("outer1", function($scope) {
$scope.$on("newmessage", function() {
        console.log("outer1");
});
$scope.$broadcast("newmessage");
});

It's a timing issue - the children are initialized after the parent had already fired $broadcast. 这是一个定时问题-孩子们在父母已经解雇了$ broadcast之后进行初始化。 If you delay $broadcast using $timeout , the children will receive the event ( update fiddle ): 如果您使用$timeout延迟$broadcast ,则子代将收到事件( update fiddle ):

app.controller("outer1", function($scope, $timeout) {
    $scope.$on("newmessage", function() {
            console.log("outer1");
    });
    $timeout(function() { // delay $broadcast using $timeout
        $scope.$broadcast("newmessage");
    }, 0);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM