I was able to get data binding working properly with angular.js with the following code.
What I'm trying to achieve:
Make my $scope.master an array, that when the user clicks "save", the value in the form field is appended or pushed to the $scope.master array.
What I've tried already:
I've tried making $scope.master an array, by declaring it as:
$scope.master = []
or
$scope.master = [{}]
or
$scope.master = {[]}
and I've also tried changing my "save method to the following:
$scope.update = function(user) {
$scope.master.push(angular.copy(user));
};
and also
$scope.update = function(user) {
$scope.master = $scope.master.toString() + angular.copy(user).toString();
};
But that doesn't seem to work either.
Current Code
Here is the working version of the code, where $scope.master gets a deep copy value of the form variable, and it doesn't add it to a list.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example29-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
</head>
<body ng-app="formExample">
<div ng-controller="ExampleController">
<form novalidate class="simple-form">
Name: <input type="text" ng-model="user.name" /><br />
E-mail: <input type="email" ng-model="user.email" /><br />
Gender: <input type="radio" ng-model="user.gender" value="male" />male
<input type="radio" ng-model="user.gender" value="female" />female<br />
<button ng-click="reset()">RESET</button>
<button ng-click="update(user)">SAVE</button>
</form>
<pre>form = {{user | json}}</pre>
<pre>master = {{master | json}}</pre>
</div>
<script>
angular.module('formExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.master = {};
$scope.update = function(user) {
$scope.master = angular.copy(user);
};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
}]);
</script>
</body>
</html>
I've really don't know how to make $scope.master a list that gets updated, every time i click "save" instead of having it be a deep copy.
Can someone please explain how to go about doing this?
Thank you for your time & help!