DiPix DiPix - 5 months ago 26
AngularJS Question

How can I reset data to default in Bootstrap modal using AngularJS?

How can I reset data to default in Bootstrap modal using AngularJS?
This is how am I trying to do:

$scope.initial = [
{
name: 'nam_fruit',
price: 0
}];

$scope.reset = function(){
$scope.dataFruit = $scope.initial;
}


But it doesn't work. Fields still have old data. Check my fiddle:
http://jsfiddle.net/q69yckt1/

hsh hsh
Answer

You are assigning an array as initial model. just remove [] and that should work:

$scope.initial =
{
  name: 'nam_fruit',
  price: 0
};

$scope.reset = function(){
  $scope.dataFruit = angular.copy($scope.initial);  
} 

as devqon mentioned in his comment you should also create a copy of $scope.initial as its just a reference copy if you just use $scope.dataFruit = $scope.initial;.

This is the working fiddle.