ItSolutionStuff.com

How to Push Item to 0 Index or First of $scope Object in AngularJS?

By Hardik Savani • October 20, 2023
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().

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>

Tags: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP AngularJS Add Remove Input Fields Dynamically Example

Read Now →

AngularJS Ajax Autocomplete Using Typeahead in PHP Example

Read Now →

AngularJS Convert Comma Separated String to Array Example

Read Now →

AngularJS - How to Create Read More/Less Toggle using Directive?

Read Now →

How to Remove # from URL in AngularJS?

Read Now →

How to Hide Div After Some Time in AngularJS?

Read Now →

How to Call Function on Page Load in AngularJS?

Read Now →

AngularJS Sorting(using Orderby Filter) Table Data Example

Read Now →

How to Change Date Format using Date Filter in AngularJS?

Read Now →

AngularJS - How to Limit String Length using Filter?

Read Now →

AngularJS - How to Capitalize the First Letter of Word Example

Read Now →

How to Remove HTML Tags from String in AngularJS?

Read Now →

PHP AngularJS CRUD with Search and Pagination Tutorial

Read Now →

Angular $scope.form Value undefined on Submit in Ionic Framework

Read Now →