简体   繁体   English

$ rootScope。$ broadcast未在ui路由器中接收

[英]$rootScope.$broadcast not picked up in ui router

I'm using ui-router to display 2 ui-views, one within the other. 我正在使用ui-router显示2个ui视图,一个在另一个视图中。 They are organized like this: 它们的组织方式如下:

.state('itemAbstract', {
    url: '/items',
    abstract: true,
    templateUrl: 'client/views/item.ng.html',
    controller: 'moveCtrl',
})
.state('item', {
    url: "/:itemId",
    parent: "itemsAbstract",
    views: {
        "otherpage":{
            templateUrl: 'client/views/other-page.ng.html',
            controller: 'otherPageCtrl'
        }
    }
})

I run the folloowwing in the otherpage controller when an item is clicked. 单击项目时,我在otherpage控制器中运行以下操作。

$rootScope.$broadcast("somethingClicked",obj)

I try to listen for the event in the item controller: 我尝试在item控制器中监听事件:

$scope.$on("somethingClicked",function(a,b){
    console.log(a)
    console.log(b);
})

Unfortunately, this function never gets called. 不幸的是,这个函数永远不会被调用。 I tried putting this listener function in the otherpage controller, and it was called correctly when the event happened. 我试着将此侦听器函数放在otherpage控制器中,并在事件发生时正确调用了它。 For some reason, though, this broadcast isn't getting transferred across scopes. 但是,由于某种原因,该广播不会跨示波器传输。 That was the whole reason I was using this, to trigger an action in the parent when something in the parent is clicked. 这就是我使用它的全部原因,当单击父级中的某项时触发父级中的操作。 Any ideas on why this is not working? 有什么想法为什么不起作用?

Here is my controller for the item 这是我的项目负责人

angular.module('mm').controller('itemCtrl',
function($scope, $meteor, $rootScope, $state, $stateParams) {
    var s = $scope;
    s.rs = $rootScope;

    $scope.$on("somethingClicked",function(a,b){
        console.log("there as a click")
        console.log(a)
        console.log(b);
    })
}

I used Batarang to debug and found that despite this code, $scope is not even registering an event listener . 我使用Batarang进行调试,发现尽管有此代码,但$ scope 甚至都没有注册事件监听器 $scope.$$listeners does not have a listener for the somethingClicked event. $scope.$$listeners 具备的somethingClicked事件的监听器。 Very strange, and it doesn't make sense why this isn't working. 很奇怪,这为什么不起作用没有意义。

Maybe you have independent controllers with no inheritance applied. 也许您有没有应用继承的独立控制器。 Inheritance on $scope is declared simply by nesting the controllers on your view. 只需在视图中嵌套控制器即可声明$ scope的继承。 In that case you may use $rootscope to broadcast or listen to event as: 在这种情况下,您可以使用$ rootscope以以下方式广播或收听事件:

//ctrl1    
$rootScope.$broadcast("somethingClicked",obj);

//ctrl2

$rootScope.$on("somethingClicked",function(a,b){
    console.log(a)
    console.log(b);
});

Take a look at this simple demo as well as an older question on Stack Overflow. 看一下这个简单的演示以及有关Stack Overflow的一个较旧的问题

EDITED Based on your sample code I had no problem at all communicating using $rootscope . 根据您的示例代码编辑我有一点问题都没有使用通信$rootscope

The state declaration use an abstract parent controller and a fetched child controller mapped into the view as: 状态声明使用一个抽象的父控制器和一个获取的子控制器映射到视图中,如下所示:

$stateProvider
        .state('test', {

          url: '/test',
          controller: 'pctrl',
          views: {
            'main': {
              template: '<div ng-controller="pctrl">{{test}}</div>' +
                '<div ui-view="childview"></div>'
            }
          }
        })
        .state('test.child', {
          url: '/test/child',
          controller: 'cctrl',
          views: {
            'childview': {
              template: '<div ng-controller="cctrl" />'
            }
          }
        });

Here is a full working demo 这是一个完整的工作演示

Answer found - I had misconfigured my routes file, and had the wrong controller specified for the page I was loading. 找到答案-我错误地配置了路由文件,并且为我正在加载的页面指定了错误的控制器。 That's why none of my event listeners registered! 这就是为什么我的事件监听器都没有注册的原因!

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

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