【问题标题】:why can not the controller of the required directive be found in angularjs?为什么在angularjs中找不到所需指令的控制器?
【发布时间】:2013-10-09 01:47:23
【问题描述】:

我在angularjs中写了两个指令,一个嵌入另一个。

以下是指令的脚本:

module.directive('foo', [
   '$log', function($log) {
     return {
      restrict: 'E',
      replace: true,
      transclude: true,
      template: '<div id="test" ng-transclude></div>',
      controller: function($scope) {
        this.scope = $scope;
        return $scope.panes = [];
      },
      link: function(scope, element, attrs) {
        return $log.log('test1', scope.panes);
      }
    };
  }
]);

module.directive('bar', [
  '$log', function($log) {
    return {
      restrict: 'E',
      replace: true,
      transclude: true,
      require: '^?foo',
      controller: function($scope) {
        return this.x = 1;
      },
      template: '<div ng-transclude></div>',
      link: function(scope, element, attrs, fooCtrl) {
        return $log.log('test2', fooCtrl);
      }
    };
  }
]);

下面是一段html:

<foo ng-controller="IndexController">
      <bar></bar>
    </foo>

下面是生成的元素,通过 chrome 开发者工具检查

<div id="test" ng-transclude="" ng-controller="IndexController" class="ng-scope">
      <div ng-transclude="" class="ng-scope"></div>
    </div>

下面是 chrome 控制台输出:

test2 
Array[0]
length: 0
__proto__: Array[0]
 angular.js:5930
test1 
Array[0]
length: 0
__proto__: Array[0]  

问题: 子指令无法获取父指令的控制器,因此“bar”链接函数的第四个参数“fooCtrl”是一个空数组。我做错了什么?

更新和回答:

我终于找到了导致奇怪结果的原因。这只是我犯的一个愚蠢的错误:

 // in directive "foo"
 controller: function($scope) {
    this.scope = $scope;
    // This line below is wrong. It is here 
    // because I transcompiled coffeescript to js.
    // return $scope.panes = [];
    // It should be like below:
    $scope.panes = []
    // I should have written .coffee like below
    // controller: ($scope) ->
    //     @scope = $scope
    //     $scope.panes = []
    //     return # prevent coffeescript returning the above expressions.
    //     # I should rather have added the above line
  }

纠正错误后,我尝试了,发现没有什么可以阻止使用控制器或在子指令中提供空内容。

【问题讨论】:

    标签: javascript angularjs angularjs-directive


    【解决方案1】:

    AFAIK,子指令中不能有控制器。

    演示:http://plnkr.co/edit/kv9udk4eB5B2y8SBLGQd?p=preview

    app.directive('foo', [
       '$log', function($log) {
         return {
          restrict: 'E',
          replace: true,
          transclude: true,
          template: '<div id="test" ng-transclude></div>',
          controller: function($scope) {
            $scope.panes = ['Item1','Item2','Item3'] 
            return { 
              getPanes: function() { return $scope.panes; }
            };
          },
          link: function(scope, element, attrs, ctrl) {
            $log.log('test1', ctrl, ctrl.getPanes(), scope.panes);  
          }
        };
      }
    ]);
    

    我删除了子控制器。

    app.directive('bar', [
      '$log', function($log) {
        return {
          restrict: 'E',
          replace: true,
          transclude: true,
          require: '^?foo',
          template: '<div ng-transclude></div>',
          link: function(scope, element, attrs, ctrl) {
            scope.x = 1;
            $log.log('test2', ctrl, ctrl.getPanes(), scope.panes, scope.x);
          }
        };
      }
    ]);
    

    【讨论】:

    • 调试了半天,我把child指令里面的controller去掉了,在html里面的child指令里面加了一些内容,比如:我是个,和我得到了我想要的结果。
    • 太棒了!很高兴我能帮上忙。
    • 我是个新手,很抱歉在 5 分钟编辑时间内忘记说谢谢 :)。顺便说一句,使用必需指令的控制器作为链接参数时,两个子指令都不能有控制器并且不能为空吗?有没有文件提到这个?
    • 对我所犯的错误深表歉意。真正的原因如下: function($scope) { this.scope = $scope;返回 $scope.panes = []; // 应该只是“返回;”或“return this;”}我犯了这个错误,因为 .js 是从 .coffee 翻译的,我忘记在最后添加一个空的 return 语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多