[英]Accessing angular's rootScope from within a directive template
I have seen some suggestions on trying to achieve this, but I can't seem to find a direct solution to what I need. 我已经看到了一些有关尝试实现此目标的建议,但是我似乎无法找到我所需的直接解决方案。
I have certain values stored in arrays and objects in the rootScope in my app, I would like to be able to access these from different directives templates in my project. 我在应用程序的rootScope中将某些值存储在数组和对象中,我希望能够从项目中的不同指令模板访问这些值。
You need to inject the $rootScope
into your directive as following, 您需要按如下所示将$rootScope
注入到指令中,
myApp.directive('testDirective', function($rootScope) {
template: '<div>Rootscope value for name is '+ $rootScope.name+'</div>'
//other directive code
});
You will be then able to access the $rootScope
values inside the directive template or even inside the template referenced by templateUrl
然后,您将能够访问指令模板内甚至$rootScope
templateUrl引用的templateUrl
内的$rootScope
值。
The simplest way I could think of would be to use rootScope in your directive and store a reference to the object you need on the directive's own scope. 我想到的最简单的方法是在指令中使用rootScope并在指令自己的作用域上存储对所需对象的引用。 You could then access it like any other item on your directive's scope. 然后,您可以像访问指令范围内的任何其他项一样访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.