我知道这是一个反复出现的问题,但不幸的是,我找不到合适的答案。
基本上,我从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
如果不想将标志放在数据对象上,则需要使用单独的对象来存储它们。使用WeakMaps,您可以轻松地将数据对象或元素本身与flags对象相关联。如果你的目标是旧的浏览器,你需要找到一种类似的方法来将数据对象/或元素与标志对象相关联
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>
我改为使用ng-show,但希望这能证明一种更好的方法:
http://plnkr.co/edit/63Io7k1mJcfppxBQUVef?p=preview
我使用的是ng-show
,我只是在需要时隐式地将"edit"附加到对象上,因为您不需要立即将其设置为true。缺少属性将意味着它返回false。
标记:
<tbody>
<tr ng-repeat="item in data">
<td ng-show="!item.edit" ng-bind="item.color"></td>
<td ng-show='item.edit'>
<input type="text" ng-model="item.color" />
</td>
<td><button ng-click="edit(item)">{{item.edit ? "Send" : "Edit"}}</button></td>
</tr>
</tbody>
对于这样的情况,我总是将视图状态封装在一个指令中。这里的意思是为每一行创建一个指令,并在该指令中移动item.edit
标志。
下面是一个非常天真的实现:
HTML:
<tbody>
<tr ng-repeat="item in data" inplace-edit="item" send-callback="send(item)"></tr>
</tbody>
JS:
app.directive('inplaceEdit', function() {
return {
restrict: 'A',
template:
'<td ng-if="!inEditMode" ng-bind="item.color"></td>' +
'<td ng-if="inEditMode">' +
'<input type="text" ng-model="item.color" />' +
'</td>' +
'<td ng-if="!inEditMode"><button ng-click="toEditMode()">edit</button></td>' +
'<td ng-if="inEditMode"><button ng-click="send()">send</button></td>',
scope: {
item: '=inplaceEdit',
sendCallback: '&'
},
link: function(scope) {
scope.inEditMode = false;
scope.toEditMode = function() {
scope.inEditMode = true;
};
scope.send = function() {
scope.sendCallback({item: scope.item});
scope.inEditMode = false;
};
}
};
});
参见分叉plunk:http://plnkr.co/edit/BS6a866aiy3BA9MX0Flx?p=preview
我想补充的是:
controllerAs
、bindToController
- 一些用于回滚/撤消更改的代码(即编辑模式下的"取消"按钮)
- 使用Angular 1.5.x和单向绑定:
item: '>inplaceEdit'
或将inplace-edit
指令与ng模型集成