这里亚去:http://plnkr.co/edit/xAgm0ny6yAB8spd5Oiai?p=preview
你也许可以简化这个和原来的值存储在隐藏字段,看看他们是否改变,并减少它每次重复都只是一个更新按钮,但这是一个开始!
<div ng-repeat="(k,v) in myCtrl.myObject">
<input type="hidden" ng-model="k">
<div>
Key: <input ng-model="newKey" ng-value="k" />
<button ng-click="myCtrl.updateKey(k, newKey, v)">Update</button>
</div>
<div>
Value: <input ng-model="v" />
<button ng-click="myCtrl.updateValue(k,v)">Update</button>
</div>
<hr />
</div>
和功能:
vm.updateKey = function(k, newKey, v) {
// newKey will be undefined if nothing changed
// Check to see if it changed
if(k != newKey && newKey !== undefined) {
// If it's empty... delete it
if(newKey != '') {
vm.myObject[newKey] = v;
}
delete vm.myObject[k];
}
};
vm.updateValue = function(k, v) {
vm.myObject[k] = v;
};
你应该考虑到一些其他的考虑是:如果用另一个新的属性名称冲突?另外 - 我的评论上面有关将其设置为undefined
是不够的。您需要使用delete
关键字,否则它不会正确更新ng-repeat。
我认为这是可能的。我会创建两个方法:'updateKey(k,v)'和'updateValue(k,v)'。在updateKey中,您需要设置'cluster.properties [k] = undefined'并将v移动到新的'cluster.properties [k]'。这是我第一次想到它时想到的......这是一个有趣的概念。我想试试这个! – Zach