简体   繁体   中英

accessing rootScope in directive

I've tried a million ways to access a rootScope variable from within this elasticui based directive with zero luck. Here is the directive, bearing in mind $rootScope is indeed being passed into its parent controller.

var elasticui;
(function (elasticui) {
var directives;
(function (directives, rootScope) {
    var IndexDirective = (function () {
        function IndexDirective() {
            var directive = {};
            directive.restrict = 'EAC';
            directive.controller = elasticui.controllers.IndexController;
            directive.link = function (scope, element, attrs, indexCtrl, rootScope) {
            console.log("Updated Index: " + $rootScope.esIndex);
            indexCtrl.indexVM.index = scope.$eval(attrs.euiIndex);
            };
            return directive;
        }
        return IndexDirective;
    })();
    directives.IndexDirective = IndexDirective;
    directives.directives.directive('euiIndex', IndexDirective);
})(directives = elasticui.directives || (elasticui.directives = {}));
})(elasticui || (elasticui = {}));

Elsewhere I'm setting $rootScope.esIndex to the index I'd like to point to but I'm getting "$rootScope is not defined". I've tried setting the directive's scope property to false, tried setting up a watcher on $rootScope.esIndex as part of the link functions return value but no matter what I do, I can't seem to figure it out.

I think part of the problem for me is this structure is a little cryptic from a directive standpoint for a newer angular person to digest. This is a block in a far larger set of directive definitions so it's just hard for me to grasp.

Any ideas how I can easy grab $rootScope in here?

Thanks a TON in advance!

Seeing more of your code would help. Specifically when you say "$rootScope is indeed being passed". However, with this limited info why don't you try changing this line:

(directives = elasticui.directives || (elasticui.directives = {}));

to something like

(directives = elasticui.directives || (elasticui.directives = {}, rootScope));

At a glance it appears your self executing function isn't being passed the rootScope parameter it expects. Good luck!

edit: Also

console.log("Updated Index: " + $rootScope.esIndex);

Should probably not have the dollar sign.

edit: Comments have much more info!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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