我在转发器范围内设置了一个输入,以便用户可以修改该值并在该行上查看该值的计算结果 - 目的是实现类似电子表格的行为。
我想知道自动填充输入框的最佳方法是什么,以便在正确更新模型的情况下将字段设置为特定值。我设置了一个 fiddle ,试图将数字 4 塞入此处的 value 属性:
这是模板的位置:
<div ng-app>
<ol ng-controller="TestCtrl">
<li ng-repeat="a in arr">
<input type="text" ng-model="testVal" value="4"></input>
<span>Value: {{testVal}}, Times {{a}}: {{testVal * a}}</span>
</li>
</ol>
</div>
这是javascript:
function TestCtrl($scope) {
$scope.arr = [1, 2, 3, 4];
}
有没有更好的方法来解决这个问题?
最佳答案
ngInit
可能有用:
<input type="text" ng-model="testVal" ng-init="testVal = a">
示例:http://jsfiddle.net/BVRzh/3/
如果我没理解错的话,这是唯一可以使用 ngInit
的情况。文档引用:
The only appropriate use of ngInit for aliasing special properties of ngRepeat, as seen in the demo bellow. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.
关于javascript - 在中继器范围上设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19319209/