AngularJS:如何从相同的指令控制器访问指令范围?

时间:2022-10-31 12:14:29

HTML

<authorname skim="skim"></authorname>

Directive

.directive('authorname', function() {
    return {
      restrict: 'E',
      scope: { 
        skim: '=skim' 
      },
      controller: function($scope, User) {
        // console.log('skim.author: ', skim.author); doesn't work
        // console.log('$scope.skim.author: ', $scope.skim.author); doesn't work
        // console.log('$scope.skim: ', $scope.skim); undefined
        User.get({ _id: skim.author }, function(user) {
          $scope.author = user.name;
        });
      },
      template: '<small>Skim by {{author}}</small>' // but can access {{skim.author}} here
    };
  });

I can access skim.author in the template, but not in the controller (which is where I need it). How can I access it in the controller?

我可以在模板中访问skim.author,但不能在控制器中访问(这是我需要它的地方)。如何在控制器中访问它?

1 个解决方案

#1


2  

I believe you are setting skim object asynchronously from the parent controller, possibly from another ajax call. But your directive would have instantiated already (controller runs/instantiated first and then the link function). So when you try to access $scope.skim it doesn't exist yet. Binding in the template works because they are updated by angular during a digest cycle the happened after the assignment of value to skim from the parent controller. So one way you could do is to create a temporary watcher till you get the skim two way bound value populated.

我相信你是从父控制器异步设置skim对象,可能是从另一个ajax调用。但是你的指令已经实例化了(控制器首先运行/实例化,然后是链接功能)。因此,当您尝试访问$ scope.skim时,它还不存在。模板中的绑定有效,因为它们在摘要周期中通过角度更新,这是在从父控制器分配值到skim之后发生的。因此,您可以做的一种方法是创建一个临时观察者,直到您获得填充的双向绑定值。

.directive('authorname', function() {
    return {
      restrict: 'E',
      scope: { 
        skim: '=skim' 
      },
      controller: function($scope, User) {

       /*Create a temporary watch till you get skim or 
         watch skim.author according to how you are assigning*/

        var unWatch = $scope.$watch('skim', function(val){ 

           if(angular.isDefined(val)) { //Once you get skim
              unWatch(); //De-register the watcher
              init(); //Initialize
           }

        });

        function init(){
           User.get({ _id: skim.author }, function(user) {
             $scope.author = user.name;
           });
        }

      },
      template: '<small>Skim by {{author}}</small>' // but can access {{skim.author}} here
    };
  });

#1


2  

I believe you are setting skim object asynchronously from the parent controller, possibly from another ajax call. But your directive would have instantiated already (controller runs/instantiated first and then the link function). So when you try to access $scope.skim it doesn't exist yet. Binding in the template works because they are updated by angular during a digest cycle the happened after the assignment of value to skim from the parent controller. So one way you could do is to create a temporary watcher till you get the skim two way bound value populated.

我相信你是从父控制器异步设置skim对象,可能是从另一个ajax调用。但是你的指令已经实例化了(控制器首先运行/实例化,然后是链接功能)。因此,当您尝试访问$ scope.skim时,它还不存在。模板中的绑定有效,因为它们在摘要周期中通过角度更新,这是在从父控制器分配值到skim之后发生的。因此,您可以做的一种方法是创建一个临时观察者,直到您获得填充的双向绑定值。

.directive('authorname', function() {
    return {
      restrict: 'E',
      scope: { 
        skim: '=skim' 
      },
      controller: function($scope, User) {

       /*Create a temporary watch till you get skim or 
         watch skim.author according to how you are assigning*/

        var unWatch = $scope.$watch('skim', function(val){ 

           if(angular.isDefined(val)) { //Once you get skim
              unWatch(); //De-register the watcher
              init(); //Initialize
           }

        });

        function init(){
           User.get({ _id: skim.author }, function(user) {
             $scope.author = user.name;
           });
        }

      },
      template: '<small>Skim by {{author}}</small>' // but can access {{skim.author}} here
    };
  });