【发布时间】:2014-12-18 13:29:35
【问题描述】:
我需要对字符串数组进行数据绑定。我需要方向数组。
我是这样模块的:
JS
function ShoppingCartCtrl($scope) {
$scope.directions = ["a", "b", "c"];
$scope.addItem = function (item) {
$scope.directions.push(item);
$scope.item = "";
};
$scope.removeItem = function (index) {
$scope.directions.splice(index, 1);
};
}
HTML
<div ng-app>
<div ng-controller="ShoppingCartCtrl">
<br />
<table border="1">
<thead>
<tr>
<td>directions</td>
<td>Remove Item</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in directions">
<td>
<input ng-model="item" />{{$index}}
</td>
<td>
<input type="button" value="Remove" ng-click="removeItem($index)" />
</td>
</tr>
</tbody>
</table>
<br />
<table>
<tr>
<td>
<input type="text" ng-model="item" />
</td>
<td colspan="2">
<input type="Button" value="Add" ng-click="addItem(item)" />
</td>
</tr>
<tr>
<td>{{directions}}</td>
</tr>
</table>
</div>
</div>
一切都按预期工作,但我有一个我找不到的错误。当您尝试直接从输入修改值时,您是不允许的。你写了,什么也没发生(这是通过将最新版本的 Angular 放入 JSFIDDLE 中解决的)。
继续:现在您可以修改这些值,但它们不会在模型中更新。如果有人可以帮助我,那就太棒了!
你可以看到它在这个jsfiddle中工作
【问题讨论】:
标签: javascript arrays angularjs data-binding