我已经做了一些非常简单的重复一些书籍,当用户点击书本时,它会打开一个新的模式。他们可以在哪里编辑这本书。当我使用双向装订时,'显示页面'自动随着我在模式上输入而改变 - 这非常棒。AngularJS - 重置绑定对象的状态
但是我想要做的是允许用户按取消按钮,并且书的状态可以追溯到它被修改之前的状态。在Angular中这可能不会返回并重置整个$ scope.books对象?
在实际的应用程序中,这将是一个API调用,我宁愿不再调用另一个服务器,除非完全需要。有没有一种模式可以解决这个问题?
(function(){
var app = angular.module('ngModalDemo', ['ui.bootstrap'])
.controller('formController', function($scope, $modal, $log){
$scope.books = [
{ Id: 1, Name:'A Feast For Crows'},
{ Id: 2, Name:'Before they are Hanged'}
];
$scope.openModal = function (currentBook) {
var modalInstance = $modal.open({
templateUrl: 'SomeModal.html',
controller: [
'$scope', '$modalInstance', function($scope, $modalInstance){
$scope.editBook = currentBook;
$scope.saveModal = function (book) {
$modalInstance.close();
};
$scope.cancelModal = function() {
// Restore the previous state here!
$modalInstance.close();
};
}]
});
};
})
})();
<div ng-controller="formController">
<p ng-repeat="displayBook in books">
<a href="#" ng-click="openModal(displayBook)">{{displayBook.Name}}</a>
</p>
<script type="text/ng-template" id="SomeModal.html">
<form name="editForm" ng-submit="saveModal(editBook)" noValidate>
<div class="modal-header">
Name: <input ng-model="editBook.Name" required /><br />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" ng-click="cancelModal()">Cancel</button>
<button class="btn btn-info" ng-disabled="editForm.$dirty && editForm.$invalid">Save</button>
</div>
</form>
</script>
</div>