Quantcast
Channel: Is there a pattern for dealing with "Cancel" in AngularJS modal dialogs? - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by dalcam for Is there a pattern for dealing with "Cancel" in AngularJS modal dialogs?

$
0
0

Another way is to copy the model before editing it, and on cancel, restore the original. Angular Controller Code:

//on edit, make a copy of the original model and store it on scope
function edit(model){
  //put model on scope for the cancel method to access
  $scope.modelBeingEdited = model;
  //copy from model -> scope.originalModel
  angular.copy(model,$scope.originalModel);  
}

function cancelEdit(){
  //copy from scope.original back to your model 
  angular.copy($scope.originalModel, $scope.modelBeingEdited)  
}
So on open of the modal dialog, call the edit function and pass a pointer to the model you want to edit. Then make sure your modal dialog is bound to $scope.editingModel. On cancel, call the cancel function, and it will copy the original values back.

Hope that helps someone!


Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>