簡體   English   中英

Angular JS廣播沒有進入子范圍

[英]Angular JS broadcast not getting inside child scope

請參考-http://jsfiddle.net/U3pVM/18701/

在控制台中,我只看到“ outer1”正在記錄,內部作用域未記錄。 缺少什么嗎?

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

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

這是一個定時問題-孩子們在父母已經解雇了$ broadcast之后進行初始化。 如果您使用$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