AngularJS事件不会从$ rootScope射击 [英] AngularJS Event not firing from $rootScope

查看:92
本文介绍了AngularJS事件不会从$ rootScope射击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有其中$ rootScope $广播事件不会被解雇一个问题:

I've got a problem where $rootScope.$broadcast events aren't being fired:

App.run(function($rootScope){
    var text = 'Not So Static Now';
    $rootScope.$broadcast('event:staticText', text);
}).$inject = ['$rootScope'];


App.controller('Ctrl1', function($scope, $rootScope) {
   $scope.staticText = "Static Text";

    var things = [
        'AngularJS',
        'StackOverflow',
        'JSFiddle'
    ];

    $scope.$emit('event:things', things);

    $scope.$on('event:staticText', function(e, args){
        $scope.staticText = args;
    });

}).$inject = ['$scope', '$rootScope'];

以上应{{STATICTEXT}}输出更改为不那么静态现在',但事实并非如此。

The above should change the {{staticText}} output to 'Not so Static Now' but it doesn't.

我创建了一个到的jsfiddle演示该问题 http://jsfiddle.net/flavrjosh/uXzTV/

I've created a JSFiddle to demonstrate the problem http://jsfiddle.net/flavrjosh/uXzTV/

这是一个更大的问题,我想在这里调试页面被刷新后,IE9不触发事件的一部分(作品第一次,但是,刷新 - F5或刷新按钮,没有任何反应)。

This is part of a bigger issue I'm trying to debug where IE9 isn't firing the events after the page gets refreshed (works first time but on refresh - F5 or the refresh button nothing happens).

任何帮助/建议,将AP preciated

Any help / Advice would be appreciated

推荐答案

看来,造成儿童范围不是设置问题的时候$ rootScope。$广播事件。

It appears that the problem's caused by the Child scopes not being setup when the $rootScope.$broadcast event is fired.

我用解决例如:

App.run(function($rootScope, $timeout){
    var text = 'Not So Static Now';

    $timeout(function(){
        $rootScope.$broadcast('event:staticText', text);
    }, 100);
}).$inject = ['$rootScope'];

App.controller('Ctrl1', function($scope, $rootScope) {
    $scope.staticText = "Static Text";

    var things = [
        'AngularJS',
        'StackOverflow',
        'JSFiddle'
    ];

    $scope.$emit('event:things', things);

    $scope.$on('event:staticText', function(e, args){
        $scope.$apply(function(){
            $scope.staticText = args;
        });
    });

}).$inject = ['$scope', '$rootScope'];

由此可以看出这里

which can be seen here

不知道这是最好的解决方案,但它的工作原理。

Not sure if this is the best solution but it works.

这篇关于AngularJS事件不会从$ rootScope射击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆