2015-02-17 3 views
2

Я не могу получить доступ к $ rootScope внутри поставщика. Я искал много вариантов угловых других модулей. это то же самое, что и я. Что не так? он пытался urload в виде отдельной функции (по аналогии с другой функцией ПолучитьЗначение) она не работает

Ошибка в $ испускают неопределен

define(['angularAMD'], function() { 

     var contextServiceModule = angular.module('contextService', []); 

     var contextService = function() { 

      var context = {}; 

      this.$get = ['$rootScope', function ($rootScope) { 
       console.log($rootScope); 
       return function(){ 
        return { 
         init: init, 
         getValue: getValue, 
         setValue: setValue, 
         urlLoad: function() {      
          $rootScope.$emit('onInit', {});/// error here 
         } 
        }; 
       }; 
      }]; 

      this.init = function() { 
       return context; 
      }; 

      this.getValue = function (key) { 
       var value = context[key] ? context[key] : null; 
       return value; 
      }; 

      this.setValue = function (key, value) { 
       context[key] = value; 
      }; 



     } 

     contextServiceModule.provider('contextService', contextService); 

    }); 

ответ

6

Вы не можете вводить $ rootScope в функции провайдера $ получить, потому что это не так но доступный. Однако вы можете вводить его вручную при вызове функции.

this.$get = ['$injector', function ($injector) { 
    return function(){ 
     return { 
      init: init, 
      getValue: getValue, 
      setValue: setValue, 
      urlLoad: function() { 
       var $rootScope = $injector.get('$rootScope'); 
       console.log($rootScope); 
       $rootScope.$emit('onInit', {});/// error here 
      } 
     }; 
    }; 
}]; 
1

Вот как я получал $ rootScope транслировать сообщения от React.js к Angular. Если ваш ng-view не расположен на корпусе, используйте вместо него.

function $broadcast(event, args]) { 
angular.element('body').injector().invoke([ 
    '$rootScope', 
    '$timeout', 
    ($rootScope, $timeout) => 
    { 
     args.unshift(event); 
     $timeout(() => { 
      $rootScope.$broadcast.apply($rootScope, args); 
     }); 
    } 
]); 

}

Понял здесь: https://www.bimeanalytics.com/engineering-blog/you-put-your-react-into-my-angular/

Смежные вопросы