Angularjs - $rootScope in directive link function

后端 未结 6 586
庸人自扰
庸人自扰 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:14

    Sometimes I have to use $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;
        });
      };
    }]);
    

提交回复
热议问题