How to push item to 0 index or first of $scope object in AngularJS?

By Hardik Savani | February 3, 2016 | Category : JSON Angular

you are working on angular JS and you wanted to add item on top of array, i mean you need to add 0 index of $scope array then you can do. in following example you can see how to do.if you want to push first then you have to use splice() instend of push().




<title>Angular JS</title>

<script src=""></script>

<script src=""></script>


app.controller("myCtrl", function($scope) {

$scope.arrayMy = [4,3,2,1];

$scope.addIndex = function () {

var t = $scope.arrayMy.length + 1;

$scope.arrayMy.splice(0, 0, t);





<body ng-app="myApp">

<div ng-controller="myCtrl">


<li ng-repeat="a in arrayMy">{{ a }}</li>


<input type="button" ng-click="addIndex()" value="Add Index" />




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.