簡體   English   中英

$ rootScope。$ emit不工作

[英]$rootScope.$emit not working

我正在嘗試使用以下方法將AngularJS中的事件發送到第二個控制器:

第一控制員:

    $scope.checkThemeContent = function(theme) {
    console.log("Trying to get cards for theme id: " +theme.id);
    console.log(theme);

    $scope.selectedTheme = theme;

    if(theme.count_of_cards >0) {
        //$scope.$emit('detailDisplayed', {});
        $scope.displayedTemplate = 'detail';
        $rootScope.$emit('detailDisplayed', {});
    } else {
        $scope.displayedTemplate = 'empty';
    }
};

第二控制器:

 $rootScope.$on('detailDisplayed', function(event, args) {
        alert('Received');
        $scope.initDataGrid();
    });

但事件不會被觸發。

我試圖在反方向使用范圍,它的工作原理。

請問哪里有問題?

我按照本教程:

http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/

謝謝你的幫助。

我試圖在反方向使用范圍,它的工作原理。

聽起來像我的代碼是在$on處理程序附加到$rootScope之前將事件發送到的位置。 確保在發出事件之前已實例化第二個控制器。

記住:發射上升到原型鏈,廣播下降。 如果你已經在頂部(即$rootScope )它不能更高。 使用$broadcast來讓事件全面展開。

$rootScope.$broadcast('detailDisplayed');

查看這篇文章:

https://blog.safaribooksonline.com/2014/04/08/refactoring-angularjs-get-hands-filthy/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM