admin管理员组文章数量:1301510
I am trying to access a variable from my $rootScope
in a directives template. However, I can't access it.
The simplified template:
<div class="item">
{{ serverRoot }}
</div>
And the directive:
ItemModule.directive('item', [function ($rootScope) {
return {
restrict: 'E',
scope: {
item: '='
},
templateUrl: 'js/modules/items/directives/templates/item.html',
link: function (scope, iElement, iAttrs) {
}
};
}])
How can I access the variable $rootScope.serverRoot
?
I am trying to access a variable from my $rootScope
in a directives template. However, I can't access it.
The simplified template:
<div class="item">
{{ serverRoot }}
</div>
And the directive:
ItemModule.directive('item', [function ($rootScope) {
return {
restrict: 'E',
scope: {
item: '='
},
templateUrl: 'js/modules/items/directives/templates/item.html',
link: function (scope, iElement, iAttrs) {
}
};
}])
How can I access the variable $rootScope.serverRoot
?
- btw rather define ItemModule.directive('item', ['$rootscope', function ($rootScope) {, or use a grunt task to avoid issues when minifying javascript. – Braulio Commented Jun 19, 2014 at 15:01
3 Answers
Reset to default 8You have defined a new scope for your directive with
scope: {
item: '='
},
So it won't be part of your link -> scope or controller -> scope. You should be able to access it via
link: function (scope, iElement, iAttrs) {
scope.serverRoot = $rootScope.serverRoot;
}
Further, your actual directive declaration needs to look like
ItemModule.directive('item', [ '$rootScope', function ($rootScope) {
Try:
<div class="item">
{{ $parent.myServerRoot }}
</div>
Although this works, I prefer Brad's solution (+1).
Have you tried?
link: function (scope, iElement, iAttrs) {
scope.myServerRoot = $rootScope.serverRoot;
}
and in the HTML
<div class="item">
{{ myServerRoot }}
</div>
本文标签: javascriptrootScope variable in directive templateStack Overflow
版权声明:本文标题:javascript - $rootScope variable in directive template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741678481a2392026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论