2014-10-28 79 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