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

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

Today, We want to share with you AngularJS push item to 0 index or first of $scope object.
In this post we will show you ng-repeat $index for a form field array, hear for How to Add and Remove items in array using AngularJS we will give you demo and example for implement.
In this post, we will learn about Push New Elements Inside an ngRepeat Array from AngularJS $scope with an example.

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

We have used CDN(libes) for AngularJS so you must need Your internet Data connection for them to work on push item to 0 index or first of $scope object in AngularJS?.

Also Read :  Angularjs Convert string to integer Example

<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>

index.html

This is the main HTML view of our Simple Web Application. In here, we have init Base declared all our simple my this Example(AngularJS push item to first or 0 index of $scope array ) dependencies and the HTML Elements that we have created.

<body ng-app="pakaApp">
	<div ng-controller="pakaCtrl">
	  	<ul>
	  		<li ng-repeat="a in pakaArray">{{ a }}</li>
	  	</ul>
	  	<input type="button" ng-click="newIndex()" value="Add Index" />
	</div>
</body>

index.js

This contains our simple custom index.js files Add Like as javascript, vuejs scripts

	<script>
		app.controller("pakaCtrl", function($scope) {
		    $scope.pakaArray = [4,3,2,1];
			$scope.newIndex = function () {
		        var t = $scope.pakaArray.length + 1;
		        $scope.pakaArray.splice(0, 0, t);
		    };
		});
	</script>

Full Example – AngularJS push item to first or 0 index

<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("pakaCtrl", function($scope) {
		    $scope.pakaArray = [4,3,2,1];
			$scope.newIndex = function () {
		        var t = $scope.pakaArray.length + 1;
		        $scope.pakaArray.splice(0, 0, t);
		    };
		});
	</script>
</head>
<body ng-app="pakaApp">
	<div ng-controller="pakaCtrl">
	  	<ul>
	  		<li ng-repeat="a in pakaArray">{{ a }}</li>
	  	</ul>
	  	<input type="button" ng-click="newIndex()" value="Add Index" />
	</div>
</body>
</html>

We hope you get an idea about Push New Elements Inside an ngRepeat Array from AngularJS $scope
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

Also Read :  User Registration and Login Authentication Code using angular 6

We hope This Post can help you…….Good Luck!.

Leave a Reply

Your email address will not be published. Required fields are marked *