gpt4 book ai didi

javascript - $rootScope.$emit 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:59:07 24 4
gpt4 key购买 nike

我正在尝试使用以下方法将 AngularJS 中的事件发送到第二个 Controller :

第一个 Controller :

    $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';
}
};

第二个 Controller :

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

但是事件没有被触发。

我尝试反向使用作用域,它起作用了。

请问哪里有问题?

我遵循了本教程:

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

感谢您的帮助。

最佳答案

I tried to used scope in reverse direction and it works.

在我看来,您的代码结构是在 $on 处理程序附加到 $rootScope 之前触发事件的位置。确保在发出事件之前已实例化第二个 Controller 。

关于javascript - $rootScope.$emit 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26766824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com