I ask this question because I do not quite understand how to think about root crops, as a dependency passed to directives.
I have a directive that should display some information from $ rootScope ...
It seemed to me that I needed to pass the $ rootScope directive to the directive, but when I write such a directive, it seems to work.
.directive("myBar", function () { return { restrict: "E", transclude: true, replace: true, template: '<div>' + '<span ng-transclude></span>' + '{{rsLabels.welcome}} {{rsUser.firstName}}!' + '</div>' } })
When do you need to do this?
.directive("myBar", function ($rootScope) { return { restrict: "E", transclude: true, replace: true, template: '<div>' + '<span ng-transclude></span>' + '{{rsLabels.welcome}} {{rsUser.firstName}}!' + '</div>' } })
Can I and HOW to use rootScope if I need it in the directive link function , or should I do it in the directive controller?
.directive("myBar", function ($rootScope) { return { restrict: "E", transclude: true, replace: true, link: function (scope, element, attrs, rootScope) { rootScope.rsUser = { firstName: 'Joe' }; rootScope.rsUser = { welcome: 'Welcome' }; }, template: '<div>' + '<span ng-transclude></span>' + '{{rsLabels.welcome}} {{rsUser.firstName}}!' + '</div>' } })
My rootScope data is defined in the start function
.run(function ($rootScope) { $rootScope.rsLabels = { welcome: 'Welcome' }; $rootScope.rsUser = { firstName: 'Joe' }; });
Thank!
angularjs angularjs-scope angularjs-directive rootscope
GRowing Feb 12 '14 at 18:44 2014-02-12 18:44
source share