动态创建的AngularJS ng-repeat指令不起作用

时间:2014-10-28 08:25:08

标签: angularjs

我试图在其他指令的编译方法中添加ng-repeat指令,但它不起作用。

这是一个演示:http://jsfiddle.net/yiooxir/mdptnqo5/1/

我期望'field'指令在三个字段(输入)中富有成效,但这不会发生

这是我的代码:

HTML:

<div ng-app='myApp' ng-controller='testCtrl'>
   {{object.var}}
   <field value='object.var' plural></field>
</div>

JS:

app.controller('testCtrl', function ($scope) {
   $scope.object = {
      var: [1, 2, 3]
   }
 });

app.directive('field', function($parse) {
  return {
    restrict: 'AE',
    scope: {
        value: '='
    },
    template: "<div><input type='text' ng-model='value'></div>",
    link: function() {}
  }
})

app.directive('plural', function($compile){
  return {
    priority: 1001,
    compile: function(element, attr) {
        element.attr({
            'ng-repeat': 'i in object.var track by $index',
            'value': 'object.var[$index]',
            'button': ''
        });
    }
  }
})

1 个答案:

答案 0 :(得分:1)

在编译函数结束时的复数指令


    return function(scope, element) { 
      $compile(element.contents())(scope);
    }