2

I can't understand how to push object into an array I tried few different ways and still can't figured it out.

var app = angular.module('myApp',[])
app.controller('dropdown', function($scope, $http){
	$scope.userInfo = [];
	
	$scope.pushInArray = function() {
		$scope.userInfo.push($scope.users)
	}

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="dropdown">

<input type="text" name="name" ng-model="users.name" placeholder="Name">
<input type="text" name="email" ng-model="users.email" placeholder="Email">
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number">
<button ng-click="pushInArray()">Add</button>
<pre>{{userInfo}}</pre>
</div>

at click of add button I push the users information in userInfo properities. I works on first time but If I modified the the value already stored value also modified(after push value is modifying).

3 Answers 3

2

try angular.copy, this will copy the exist object with a new instance.

var app = angular.module('myApp',[])
app.controller('dropdown', function($scope, $http){
	$scope.userInfo = [];
	
	$scope.pushInArray = function() {
    var user = angular.copy($scope.users);
		$scope.userInfo.push(user);
	}

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="dropdown">

<input type="text" name="name" ng-model="users.name" placeholder="Name">
<input type="text" name="email" ng-model="users.email" placeholder="Email">
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number">
<button ng-click="pushInArray()">Add</button>
<pre>{{userInfo}}</pre>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You need to empty yours users before setting it to new values:

$scope.userInfo = [];

$scope.pushInArray = function(data) {
    $scope.userInfo.push(data)
    $scope.users = null;
}

HTML:

<input type="text" name="name" ng-model="users.name" placeholder="Name">
<input type="text" name="email" ng-model="users.email" placeholder="Email">
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number">
<button ng-click="pushInArray(users)">Add</button>
<pre>{{userInfo}}</pre>

Here is the working Plnkr

9 Comments

Its working fine, Is there any way to do without making users properities as null
Why you don't want to set it to null?
just I want to know is there any way
@AayushiJain yea I forgot to null it haha nice answer bud :)
@vinox: I don't think you can make it without setting it to null as the name/email/phoneNo values will persist in users unless you reinitialize them
|
0

You need to pass the object to the scope function to persist it.

var app = angular.module('myApp',[])
app.controller('dropdown', function($scope, $http){
    $scope.userInfo = [];

    $scope.pushInArray = function(data) {
        var entry = (JSON.parse(JSON.stringify(data)));
        $scope.userInfo.push(entry);
    }

});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="dropdown">

<input type="text" name="name" ng-model="users.name" placeholder="Name">
<input type="text" name="email" ng-model="users.email" placeholder="Email">
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number">
<button ng-click="pushInArray(users)">Add</button>
<pre>{{userInfo}}</pre>
</div>

5 Comments

same thing happening after first value push If I modified input text value pushed object value also modified
yea I forgot to add the $scope.users = null to clear it as per @Aayushi's answer
can I know the reason why stringify the data and again parse the data
instead of doing, you can use default angular function angular.copy to achieve. refer @Pengyy answer
It's just if you wanted to do vanilla JavaScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.