【发布时间】:2016-05-17 15:30:54
【问题描述】:
我知道这是一个反复出现的问题,但很遗憾我找不到适合我的情况的答案。
基本上,我从 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}];
});
提前致谢!
【问题讨论】:
-
在此处包含相关代码,链接可能会失效,我们不应该去外部站点查看代码。
标签: javascript angularjs json angularjs-ng-repeat angularjs-ng-switch