如果我有一个 ng-repeat 指令与我的初始数据绑定(bind),
<!-- list 1-->
<li ng-repeat="data in datas">{{data.name}}</li>
我通过另一个 ng-repeat 和 ng-model 指令更改数据,
<!-- list 2-->
<li ng-repeat="data in datas">
<input type="text" ng-model="data.name">
</li>
在 Angular 方式中,任何方法都可以不立即执行 list 1 ng-repeat 数据刷新(在我单击“保存”按钮后)?
<button ng-click="save()">Save</button>
最佳答案
您可以使用第二个(临时)克隆进行更改,并使用 angular.copy 将更改复制到实际对象。 。
The actual list:
<ul><li ng-repeat="item in items">
{{item.name}} (id: {{item.id}})
</li></ul>
Edit the list:
<ul><li ng-repeat="item in tempCopy">
<input type="text" ng-model="item.name" />
</li></ul>
<button ng-click="persistChanges()">Save</button>
<button ng-click="discardChanges()">Discard</button
在你的 Controller 中:
/* Persist the changes */
$scope.persistChanges = function () {
angular.copy($scope.model.tempCopy, $scope.model.items);
};
/* Discard the changes */
$scope.discardChanges = function () {
angular.copy($scope.model.items, $scope.model.tempCopy);
};
另请参阅此 short demo 。
最后,Angular 文档上有一个类似的示例 angular.copy 。
关于javascript - Angular js : What is the best way to let Binding Data later update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21279144/