Angularjs - 指令链接函数中的$ rootScope

GRo*_*ing 47 angularjs angularjs-directive angularjs-scope rootscope

我问这个问题是因为我不太清楚如何将rootcope视为传递给指令的依赖

我有一个指令,需要显示来自$ rootScope的一些信息......

我以为我需要将$ rootScope传递给一个指令但是当我写这样的指令时它似乎有效.

.directive("myBar", function () {
 return {
    restrict: "E",
    transclude: true,
    replace: true,
    template:   '<div>' + 
                '<span ng-transclude></span>' + 
                '{{rsLabels.welcome}} {{rsUser.firstName}}!' + 
                '</div>'
}
})
Run Code Online (Sandbox Code Playgroud)

我什么时候需要这样做?

.directive("myBar", function ($rootScope) {
 return {
    restrict: "E",
    transclude: true,
    replace: true,
    template:   '<div>' + 
                '<span ng-transclude></span>' + 
                '{{rsLabels.welcome}} {{rsUser.firstName}}!' + 
                '</div>'
}
})
Run Code Online (Sandbox Code Playgroud)

如果我需要在指令的link函数中使用rootScope,我可以使用rootScope吗?或者我应该在指令的控制器中执行它?

.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>'
}
})
Run Code Online (Sandbox Code Playgroud)

我的rootScope数据在run函数中定义

 .run(function ($rootScope) {

  $rootScope.rsLabels = { 
      welcome: 'Welcome'
  };

  $rootScope.rsUser = { 
     firstName: 'Joe'
  };
});
Run Code Online (Sandbox Code Playgroud)

谢谢!

kar*_*una 68

你可以这样做:

{{$root.rsLabels.welcome}}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,不是那样的.我使用`link:function(scope,...){scope.$ root}`从指令访问`$ rootScope`. (25认同)

Mik*_*eel 52

从我的实验经验来看,似乎因为所有$ scope最终都继承自$ rootScope,你将能够访问它上面的数据而不需要它作为服务,遵循标准的javascript原型继承规则.如果要将指令中的scope属性设置为false或{},则会发现您无法再访问它.

.directive("myBar", function($rootScope) {
    return {
        restrict: "E",
        scope: { /* Isolate scope, no $rootScope access anymore */ },
        transclude: true,
        replace: true,
        template: '<div>' + 
                  '<span ng-transclude></span>' + 
                  '{{rsLabels.welcome}} {{rsUser.firstName}}!' + 
                  '</div>'
    };
});
Run Code Online (Sandbox Code Playgroud)

示例:http://jsbin.com/bequy/1/edit

  • 您需要将$ rootScope视为全局命名空间,并且希望避免使其混乱.你可以在你的指令中注入它,但指令的整个要点是没有太多外部依赖的组件.如果您需要来自$ rootScope的东西,您可以考虑将其重构为服务. (3认同)
  • 设置范围:true将使其工作.通过将其设置为空哈希{}或false隔离范围并中断继承链.(参见指令定义对象,范围)https://code.angularjs.org/1.4.1/docs/api/ng/service/$compile(我的例子是针对1.2.1但它没有改变我的能力告诉). (3认同)

Kur*_*ois 7

不建议使用根范围在角度应用程序中设置和获取属性.尝试使用$ cacheFactory,因为这样你也可以在各种请求上缓存一些值.($ cacheFactory docs)


omi*_*kes 7

有时我必须使用 $scope.$root:

app.directive('setOrdinal', function() {
  return {
    link: function($scope, $element, $attr) {

      var steps = $scope.$root.steps;

      $scope.$watch(setOrdinal, function(value) {
        if (value)
        {
          // steps code here
        }
      });
    }
  };
});

app.controller('stepController', ['$scope', '$rootScope', 'GetSteps', function ($scope, $rootScope, GetSteps) {
  var s = $scope;
  var r = $rootScope;

  s.initialize = function(id)
  {
    GetSteps.get({id: id}, function(resp){
      r.steps = resp.steps;
    });
  };
}]);
Run Code Online (Sandbox Code Playgroud)