簡體   English   中英

Angular在提供程序中注入$ rootScope

[英]Angular injecting $rootScope in provider

我無法在提供者內部訪問$ rootScope。我看了很多有角度的其他模塊實現。 它與我的實施方式相同。 怎么了? 它嘗試將urload作為一個單獨的函數(類似於其他getValue函數)它不起作用

錯誤是$ emit未定義

 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);

    });

您不能將$ rootScope注入提供者$ get函數,因為它尚不可用。 但是,您可以在調用函數時手動注入它。

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
            }
        };
    };
}];

角色提供程序在配置階段創建,$ rootScope在應用程序運行階段之前不可用。 這里有幾個Angular資源和類似的問題:

https://docs.angularjs.org/guide/module

https://docs.angularjs.org/guide/providers

https://stackoverflow.com/a/10489658/3284644

這就是我如何讓$ rootScope從React.jsAngular廣播消息。 如果你的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/

重要提示:以下解決方案在縮小時不起作用。 為此你需要進行依賴注入,但是在配置階段不能調用$ get

app.provider('someHandler', function() {
    this.$get = function ($rootScope) {
        function doBroadcast(words) {
            $rootScope.$broadcast(words);
        }

        return {
            shout: function (words) {
                doBroadcast(words);
            }
        }
    }
});

如果您需要在配置階段使用,您可以執行以下操作

app.config(['someHandlerProvider', function(someHandlerProvider) {
    someHandlerProvider.$get().shout('listen up');
}

在運行期間你可以做一些someHandler.shout('listen up')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM