我知道这是一个反复出现的问题,但不幸的是,我找不到合适的答案来解决我的问题。
基本上,我从 JSON API 端点获取数据,该端点使用ng-repeat
. 我现在想要ng-switch
查看输入字段以修改数据(并稍后将其发送回服务器)。
Atm,我的解决方案取决于在数据中拥有我不喜欢的属性。我确信有比在检索数据后注入此属性更聪明的方法 - 有什么建议吗?
HTML:
<tbody>
<tr ng-repeat="item in data" ng-switch on="item.edit" >
<td ng-switch-default ng-bind="item.color"></td>
<td ng-switch-when='true'>
<input type="text" ng-model="item.color" />
</td>
<td ng-switch-default><button ng-click="switch(item)">edit</button></td>
<td ng-switch-when='true'><button ng-click="send(item)">send</button></td>
</tr>
</tbody>
JS:
var app = angular.module('myApp', []);
app.controller('MyCtrl', function($scope) {
$scope.switch = function (item) {
if (item.edit) {
item.edit = false;
} else {
item.edit = true;
}
};
$scope.send = function (item) {
if (item.edit) {
// data is sent...
item.edit = false;
} else {
item.edit = true;
}
};
$scope.data = [
{color: 'blue', edit: false},
{color: 'green', edit: false},
{color: 'orange', edit: false}];
});
提前致谢!
这是一个 plunker: http ://plnkr.co/edit/h8ar4S43JUvjHurzLgT0?p=preview