I have a isolate scope directive that I am using inside ng-repeat
, which is iterating over an array from the controller of that template. The template is as follows:
我在ng-repeat中使用了一个隔离范围指令,它从模板的控制器迭代一个数组。模板如下:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="bootstrap.min.css" />
<script src="angular.min.js"></script>
<script src="script1.js"></script>
</head>
<body ng-app="AddNewTest" ng-controller="AddNewController">
<div class="items" ng-repeat="row in rows">
<add-new-row data="row" index="$index"></add-new-row>
</div>
</body>
</html>
The directive is defined as follows:
该指示的定义如下:
angular.module('AddNewTest', []).
directive('addNewRow', function ($timeout) {
return {
controller: 'AddNewController',
link: function (scope, element, attribute) {
element.on('keyup', function(event){
if(scope.index + 1 == scope.rows.length) {
console.log('keyup happening');
$timeout(function () {
scope.rows.push({ value: '' });
scope.$apply();
});
}
})
},
restrict: 'E',
replace: true,
scope: {
index: '='
},
template: '<div class="add-new"><input type="text" placeholder="{{index}}" ng-model="value" /></div>'
}
}).
controller('AddNewController', function ($scope, $timeout) {
$scope.rows = [
{ value: '' }
];
});
But even after adding new row and doing a $apply()
the ng-repeat is not rendering the new data added. Please help.
但是,即使添加了新行并执行了$apply()之后,ng-repeat并没有呈现添加的新数据。请帮助。
Plnkr链接在这里
2 个解决方案
#1
1
Pass array of rows to directive as follow:-
将行数组传递给指令如下:-
scope: {
index: '=',
rows :'='
},
<add-new-row rows="rows" index="$index"></add-new-row>
工作恰好
#2
1
Each ng-repeat creates an isolated scope than you declare an isolated scope inside your directive that has the same controller as the div. You're swimming in the $scope soup :)
每个ng-repeat创建一个独立的范围,而不是在指令中声明一个独立的范围,该范围具有与div相同的控制器。
I would personnally make a clean and independent directive with its own controller.
我将亲自制定一个干净和独立的指令与它自己的控制器。
angular.module('AddNewTest', []).
directive('addNewRow', function () {
return {
restrict: 'E',
controller: MyController,
controllerAs: '$ctrl',
template: '{{$ctrl.rows.length}}<div class="add-new"><pre>{{$ctrl.rows | json}}</pre><input type="text" placeholder="0" ng-model="value" /></div>'
}
}).
controller('MyController', MyController);
function MyController($scope) {
var vm = this;
this.rows = [ { value: '' } ];
$scope.$watch("value",function(value){
if(value)
vm.rows.push({ value: value });
});
}
http://plnkr.co/edit/AvjXWWKMz0RKSwvYNt6a?p=preview
http://plnkr.co/edit/AvjXWWKMz0RKSwvYNt6a?p=preview
Of course you can still bind some data to the directive using bindToController
(instead of scope:{}) and if you need an ng-repeat, do it in the directive template directly.
当然,您仍然可以使用bindToController(而不是范围:{})将一些数据绑定到指令,如果您需要一个ng-repeat,那么直接在指令模板中执行它。
#1
1
Pass array of rows to directive as follow:-
将行数组传递给指令如下:-
scope: {
index: '=',
rows :'='
},
<add-new-row rows="rows" index="$index"></add-new-row>
工作恰好
#2
1
Each ng-repeat creates an isolated scope than you declare an isolated scope inside your directive that has the same controller as the div. You're swimming in the $scope soup :)
每个ng-repeat创建一个独立的范围,而不是在指令中声明一个独立的范围,该范围具有与div相同的控制器。
I would personnally make a clean and independent directive with its own controller.
我将亲自制定一个干净和独立的指令与它自己的控制器。
angular.module('AddNewTest', []).
directive('addNewRow', function () {
return {
restrict: 'E',
controller: MyController,
controllerAs: '$ctrl',
template: '{{$ctrl.rows.length}}<div class="add-new"><pre>{{$ctrl.rows | json}}</pre><input type="text" placeholder="0" ng-model="value" /></div>'
}
}).
controller('MyController', MyController);
function MyController($scope) {
var vm = this;
this.rows = [ { value: '' } ];
$scope.$watch("value",function(value){
if(value)
vm.rows.push({ value: value });
});
}
http://plnkr.co/edit/AvjXWWKMz0RKSwvYNt6a?p=preview
http://plnkr.co/edit/AvjXWWKMz0RKSwvYNt6a?p=preview
Of course you can still bind some data to the directive using bindToController
(instead of scope:{}) and if you need an ng-repeat, do it in the directive template directly.
当然,您仍然可以使用bindToController(而不是范围:{})将一些数据绑定到指令,如果您需要一个ng-repeat,那么直接在指令模板中执行它。