我已經做了一些非常簡單的重複一些書籍,當用戶點擊書本時,它會打開一個新的模式。他們可以在哪裏編輯這本書。當我使用雙向裝訂時,'顯示頁面'自動隨着我在模式上輸入而改變 - 這非常棒。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>