我想使用计数器
http://plnkr.co/edit/eRWPuEP8LdQBbHksIL3S?p=info ng-repeat 内的指令
就像
<div ng-repeat="item in items">
<div counter value="item" min="1" max="30" step="3" editable></div>
</div>
在我的 Controller 中
$scope.items = []
items.push(1);
items.push(2);
但是它不起作用。
编辑:1
在我的 Controller 中
$scope.items = []
$scope.items.push(1);
$scope.items.push(2);
编辑2:
我的Plunkr:http://plnkr.co/edit/ji2ppBp0PyRCzAT4UhMj?p=preview
最佳答案
ng-repeat 创建一个子作用域,item
作为原始类型传递到子作用域。即使您使用 value
的 2 路绑定(bind)来隔离指令中的范围,该值也不会更改,即。 scope.items
始终为 [1, 2]
解决方案是将对象(引用)传递给子作用域,以实现此目的:
$scope.items = []
$scope.items.push({val:1});
$scope.items.push({val:2});
// template of counter directive
<input type="text" class="counter-field" ng-model="value.val">
var setValue = function( val ) {
scope.value.val = parseInt( val );
};
您可能还需要更改 minus
和 plus
方法以引用 scope.value.val
关于javascript - 如何在 ng-repeat 中使用指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27756792/