Angularjs - $rootScope in directive link function

后端 未结 6 587
庸人自扰
庸人自扰 2021-01-30 10:57

I am asking this question because I am not quite clear on how to think of rootscope as a dependency passed to directives

I have a directive that needs to display some in

6条回答
  •  迷失自我
    2021-01-30 11:09

    From my experiments \ experience, it seems that since all $scopes ultimately inherit from the $rootScope you will be able to access data on it without requesting it as a service, following standard javascript prototypical inheritance rules. If you were to set the scope property in your directive to false or {} you will find that you can no longer access it.

    .directive("myBar", function($rootScope) {
        return {
            restrict: "E",
            scope: { /* Isolate scope, no $rootScope access anymore */ },
            transclude: true,
            replace: true,
            template: '
    ' + '' + '{{rsLabels.welcome}} {{rsUser.firstName}}!' + '
    ' }; });

    Example: http://jsbin.com/bequy/1/edit

提交回复
热议问题