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

AngularJS

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

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

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


Related FAQ

Here are some more FAQ related to this Article:

  1.   Remove null values from array in PHP
  2.   Laravel 6 Join multiple tables Eloquent Examples
  3.   Filter - Limit the length of string using AngularJS
  4.   Scope Variables using Angular Example
  5.   Clear All Cache From Laravel project 5.7
  6.   Event Directives with Parameters using Angular Example
  7.   ng-mouseup Event using Angular Example
  8.   laravel clear cache using Terminal/Command Prompt
  9.   how to upload image in ckeditor in Laravel?
  10.   Laravel 6 Join Clause Example Tutorial

Leave a Reply

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