简体   繁体   English

从Plain JavaScript调用Angular Service到Controller

[英]Call from Plain JavaScript to Angular Service to Controller

I am using $rootScope.$emit() for raising events from service to controller. 我使用$rootScope.$emit()将事件从服务引发到控制器。

It is working fine when emitting from the instance of service that is referenced in controller but $rootScope.$on is not getting called when emitting from the instance of service I got from angular injector in plain javascript . 当从控制器中引用的服务实例发出但$ rootScope时,它正常工作。当从我在普通javascript中从角度注入器获得的服务实例发出时,$ on不会被调用。

Following is my code 以下是我的代码

Plain JavaScript 简单的JavaScript

    var msgHandlerJS = function () {
    var injector = angular.injector(['ng', 'services']);
    var aMsgHandlerService = injector.get('msgHandlerService');

    aMsgHandlerService.TestScopeWatch();
}

Calling the above when dom is ready : 当dom准备就绪时调用上面的内容:

angular.element(document).ready(function () {    
    msgHandlerJS();
});

Service 服务

(function (module) {    

    var msgHandlerService = function ($rootScope, $http, $q, $timeout) {  
        var TestScopeWatch = function () {
            $http.get('~/test.json').then(function (result) {
                EmailPacket = result.data.Packet;

                $rootScope.$emit("EmailPacketChanged", EmailPacket);     
            });
        };

        return {
            //making public
            TestScopeWatch:TestScopeWatch,
        };
    };

    module.factory("msgHandlerService", ["$rootScope","$http", "$q","$timeout", msgHandlerService]);

}(angular.module("services")));

Controller 调节器

(function (module) {   
    function testController($rootScope,$scope, msgHandlerService) {
        $rootScope.$on("EmailPacketChanged", function(event,data){
            alert('Here I am');
        };
    };

    module.controller("testController", ["$rootScope","$scope", "msgHandlerService",  testController]);    

}(angular.module('app')));

You need to get the proper injector of the application, angular.injector creates a new injector, it has nothing to do with the injector that bootstrapped your app. 您需要获得应用程序的正确注入器, angular.injector创建一个新的注入器,它与引导您的应用程序的注入器无关。 So the instance of the service (yourService, rootScope inside it) you get from that injector is different instance than what is in the app. 因此,从该注入器获取的服务实例(yourService,rootScope)与应用程序中的实例不同。 You should instead use the getter .injector() from the rootElement of the application. 您应该使用应用程序的rootElement中的getter .injector()

ie if you have an ng-app or manually bootstrapping, get the injector out of that element. 即如果您有ng-app或手动引导,请从该元素中取出注射器。 Example: 例:

 var injector = angular.element(document.querySelector('[ng-app]')).injector();

If your app root is document ( html ) then get injector from the document ie : 如果您的应用程序根目录是文档( html ),那么从document获取注入器,即:

var injector = angular.element(document).injector()

Also if you want any scope bindings to reflect you also need to invoke a digest cycle (alert will work fine ofcourse): 此外,如果您希望任何范围绑定反映您还需要调用摘要周期(警报将正常工作):

var msgHandlerJS = function() {
   var injector = angular.element(document).injector();
   var aMsgHandlerService = injector.get('msgHandlerService');
   var $rootScope = injector.get('$rootScope');
   aMsgHandlerService.TestScopeWatch();
   $rootScope.$digest();
}

 (function(module) { var msgHandlerService = function($rootScope, $http, $q, $timeout) { var TestScopeWatch = function() { //$http.get('~/test.json').then(function(result) { // EmailPacket = result.data.Packet; $rootScope.$emit("EmailPacketChanged", {}); // }); }; return { //making public TestScopeWatch: TestScopeWatch, }; }; module.factory("msgHandlerService", ["$rootScope", "$http", "$q", "$timeout", msgHandlerService]); }(angular.module("services", []))); (function(module) { function testController($rootScope, $scope, msgHandlerService) { $rootScope.$on("EmailPacketChanged", function(event, data) { $scope.got = "got the message!!"; }); } module.controller("testController", ["$rootScope", "$scope", "msgHandlerService", testController]); }(angular.module('app', ['services']))); var msgHandlerJS = function() { var injector = angular.element(document.querySelector('[ng-app]')).injector(); var aMsgHandlerService = injector.get('msgHandlerService'); var $rootScope = injector.get('$rootScope'); aMsgHandlerService.TestScopeWatch(); $rootScope.$digest(); } angular.element(document).ready(function() { msgHandlerJS(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div class="test" ng-app="app" ng-controller="testController"> {{got}} </div> 

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

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