我知道这是一个反复出现的问题,但不幸的是,我找不到适合我案件的正确答案。
基本上,我是从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}];
});
提前致谢!
这是一个矮人:
http://plnkr.co/edit/h8ar4S43JUvjHurzLgT0?p=preview
最佳答案
如果您不想将标志放在数据对象上,则需要使用一个单独的对象来存储它们。使用WeakMaps,您可以轻松地将数据对象或元素本身与标志对象相关联。如果您以较旧的浏览器为目标,则需要找到一种类似的方法来将数据对象/或元素与标志对象相关联
JS
let map = new WeakMap();
$scope.editing = function(item){
return map.get(item).edit;
}
$scope.switch = function (item) {
let flags = map.get(item);
if (flags.edit) {
flags.edit = false;
} else {
flags.edit = true;
}
};
//Note you could combine switch and send into a single toggle function
$scope.send = function (item) {
let flags = map.get(item);
if (flags.edit) {
flags.edit = false;
} else {
flags.edit = true;
}
};
$scope.data = [
{color: 'blue'},
{color: 'green'},
{color: 'orange'}
];
//Create an empty flags object for each data item
for(let item of $scope.data){
map.set(item,{});
}
的HTML
<tr ng-repeat="item in data" ng-switch on="editing(item)" >
<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>
演示版
// Code goes here
var app = angular.module('myApp', []);
app.controller('MyCtrl', function($scope) {
var map = new WeakMap();
//Using fat arrow less code to write
$scope.editing = item=>map.get(item).edit;
//Since "switch" and "send" had similar
//toggling code just combined them
//Also no need to use if statement, just use the NOT operator
//to toggle the edit flag
$scope.toggle = item=>{
let flags = map.get(item);
flags.edit = !flags.edit;
};
$scope.switch = item=>{
$scope.toggle(item);
//Do some switching?
//if not doing anything else just
//call toggle in the ng-click
};
$scope.send = item=>{
$scope.toggle(item);
//Do some sending
};
$scope.data = [
{color: 'blue'},
{color: 'green'},
{color: 'orange'}];
for(let item of $scope.data){
map.set(item,{});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<table>
<thead>
<tr>
<th width="180">Column</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data" ng-switch on="editing(item)" >
<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>
</table><br>
"$scope.data" should never change after hitting edit/send since the flag is no longer on the data item object:
<code><pre>{{data}}</pre></code>
</div>
关于javascript - 如何在ng-repeat中使用ng-switch编辑JSON API数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37280503/