动态创建的AngularJS ng repeat指令无效
我试图在另一个指令的编译方法中添加一个ng repeat指令,但它不起作用 下面是一个演示: 我期望“field”指令在这三个字段(输入)中会有成效,但这并没有发生 这是我的密码: html:动态创建的AngularJS ng repeat指令无效,angularjs,Angularjs,我试图在另一个指令的编译方法中添加一个ng repeat指令,但它不起作用 下面是一个演示: 我期望“field”指令在这三个字段(输入)中会有成效,但这并没有发生 这是我的密码: html: <div ng-app='myApp' ng-controller='testCtrl'> {{object.var}} <field value='object.var' plural></field> </div> app.controll
<div ng-app='myApp' ng-controller='testCtrl'>
{{object.var}}
<field value='object.var' plural></field>
</div>
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': ''
});
}
}
})
{{object.var}}
js:
<div ng-app='myApp' ng-controller='testCtrl'>
{{object.var}}
<field value='object.var' plural></field>
</div>
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': ''
});
}
}
})
app.controller('testCtrl',函数($scope){
$scope.object={
变量:[1,2,3]
}
});
app.directive('field',function($parse){
返回{
限制:“AE”,
范围:{
值:'='
},
模板:“”,
链接:函数(){}
}
})
应用指令('复数',函数($compile){
返回{
优先权:1001,
编译:函数(元素,属性){
元素属性({
'ng repeat':'i in object.var track by$index',
“值”:“object.var[$index]”,
“按钮”:”
});
}
}
})
编译函数末尾的复数指令
return function(scope, element) {
$compile(element.contents())(scope);
}
返回函数(范围、元素){
$compile(element.contents())(范围);
}
对如果我们添加如下所示的代码,也可以做到这一点: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)但不清楚为什么会这样?大多数其他指令都没有重复