2016-06-21 4 views
0

나는 $ 범위를 사용하고 $ 내 ListController에서 방출 달러 (A $)의 rootScope를 트리거 할 MainController의 이벤트 (각 1.3)에 $가 :..왜 rootscope.on이 트리거되지 않습니까?

(function() { 
    'use strict'; 

    angular 
     .module('mymodule.faultlist') 
     .controller('MainController', ['$rootScope', function ($rootScope) { 
      $rootScope.$on('newViewLoaded', function (event, metadata) { 
       $rootScope.metadata = metadata; 
       console.log('hello metacontroller metadata', metadata); 
      }); 

     }]) 
     .controller('ListController', ['faultlistservice', 'faultListConstants', '$scope', '$timeout', '$rootScope', 
      function (faultlistservice, faultListConstants, $scope, $timeout, $rootScope) { 

       $timeout(function() { 
        $scope.$emit('newViewLoaded', { 
         'title': faultListConstants.meta.title, 
         'description': faultListConstants.meta.description 
        }); 
       }, 100); 

      }]); 
})(); 

이것은 index.html을 대략 모습입니다 같은 :

. 
. 
<head ng-controller="MainController"> 
<div class="container main-content" ui-view > 
</div> 
.. 

이것은 트리거되지 않습니다. $ rootScope. $ 블록을 ListController로 옮긴 다음 $ rootScope. $ on이 트리거됩니다. 왜 이것이 MainController에서 작동하지 않습니까? 아니면 이것을 구현하는 더 좋은 방법이 있습니까?

+1

@TheHeadRush 같은 질문입니다! –

+0

ListController 인 경우 MainController 부모입니까? HTML을 공유 할 수 있습니까? –

+0

예 MainController는 맨 위에 정의되어 있습니다. 즉 head 태그에 정의되어 있습니다. ListController는 ui-view (ui-router) 내에 있습니다. MainController는 stateprovider에 정의되어 있지 않습니다. –

답변

0

는 코드가 너무 <head ng-controller="MainController">이 줄 ngApp 밖에, http://plnkr.co/edit/hsl3l6rTJd1SUDgaijCy?p=preview

내가 문제가되지 HTML 태그에 당신이 한 겨-응용 프로그램에있을 수 있습니다 생각 작동합니다.

+0

좋은 픽업 감사! –

0

코드는 당신이 당신의 원래의 코드와 함께 작동하도록이 예제를 사용하는 방법을 plunkr 보여줍니다 제대로 http://plnkr.co/edit/nX9KVqY0SZ46Yvf92VAk?p=preview

작동합니다. 여기에있는 제목과 설명은 emit 함수 내에서 전달할 인수이며 문자열 리터럴이 될 수 없습니다.

$scope.$emit('newViewLoaded', { 
         'title': faultListConstants.meta.title, 
         'description': faultListConstants.meta.description 
        }) 
관련 문제