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?
You 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) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With