AngularJs add item to Array


AngularJs add item to Array- It is very simple to push a value in AngularJs Array . push() method is used to add(push) an element in AngularJs array. Here in this tutorial we are going to explain how you can use push() to insert an element in AngularJs Array. You can use our online demo try and edit the code online.


AngularJs add item to Array | Example

You can use push() method to add an element in an array. Here is full example –

AngularJs add item to Array | Example:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
 $scope.exampleArray = ['Mango','Banana'];
 $scope.pushInArray = function() {
	 // get the input value
	 var inputVal = $scope.arrInput;
	 $scope.exampleArray.push(inputVal);
	 
 };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController"> 
 
 Enter a value and click "Push" Button to insert in array.
 <input type="text" name="arrExample" ng-model="arrInput"><br>
 <button ng-click='pushInArray()' >Push</button><br>
 Array = {{exampleArray}}<br>

</div> 
</div>
</body>  
</html>  

Try it »

In the above example we have created a simple example to add elements in array in AngularJs.

If you run the above example it will produce the output something like this –

AngularJs add item to Array


Advertisements

Add Comment

📖 Read More