2014-10-28 74 views
0

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

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

我预计“场”指令是在三个字段(输入)卓有成效的,但这种情况并非如此

这里是我的代码:

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
复数指令

在编译功能

 

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

+0

是结束。如果我们添加如下所示的代码,也可以这样做:

 compile: function(element, attr) { element.attr({ 'ng-repeat': 'i in object.var track by $index', 'value': 'object.var[$index]', }); var parent = element.parent(); parent.append(element) 
但是现在还不清楚为什么这样?其他大多数指令都不能解决这个问题 – yiooxir 2014-10-28 09:37:01