How to push item to 0 index or first of $scope object in AngularJS?
In this post we will give you information about How to push item to 0 index or first of $scope object in AngularJS?. Hear we will give you detail about How to push item to 0 index or first of $scope object in AngularJS?And how to use it also give you demo for it if it is necessary.
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().
Example:
<html>
<head>
<title>Angular JS</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<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);
};
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<ul>
<li ng-repeat="a in arrayMy">{{ a }}</li>
</ul>
<input type="button" ng-click="addIndex()" value="Add Index" />
</div>
</body>
</html>
Hope this code and post will helped you for implement How to push item to 0 index or first of $scope object in AngularJS?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs