javascript - 如何在 ng-repeat 中使用指令?

标签 javascript angularjs

我想使用计数器 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 );
};

您可能还需要更改 minusplus 方法以引用 scope.value.val

关于javascript - 如何在 ng-repeat 中使用指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27756792/

相关文章:

javascript - 测试 JSONP $resource AngularJS

javascript - 暂停和恢复ajax重复请求

javascript - 替代引用对象

javascript - Angularjs 路由状态正在加载无限时间

javascript - 当 Angular JS 中的模型值发生变化时,如何防止作用域变量发生变化

javascript - 如何在 ng-repeat 中传递动态变量

javascript - 如何检测输入 jQuery 的值变化?

javascript - setTimeout 和带参数的递归函数

javascript - 为什么 browserify 在调用 require 之前加载每个模块?

css - 将错误消息设置到控件 Bootstrap 和 Angular Js 的右侧