How to Push New Elements in Between an Existing ng-repeat Array from AngularJS $scope using JavaScript splice() Method

I have written an article before on how to Push new elements inside an ng-repeat array from AngularJS $scope. In that example, I would take inputs from two textboxes and add the values in the array from my controller. Now, here in this quick post I’ll show you with an example on how to add or push new elements in between existing ng-repeat array list.

In the previous example however, the new elements would add at the end of the array list. Here is the demo of that previous example.

This is another scenario. I wish to add or push new items at a random location inside an existing ng-repeat array list. Say for example, I have a list of Five elements, and I wish to add the new element at the 3rd index (position) in the array.


This is very simple. I’ll use JavaScript splice() method to add new items to the array. All I need to know is the index or the location (position) where I want to see the new item in the list. I’ll provide values to the splice() method dynamically in my controller.

For my example, I’ll have two input boxes (textboxes) and a button. The first textbox will allow you to select a number (as index for the array), and in the second textbox you will enter a value (a product).

The Markup
<!DOCTYPE html>
    <script src=""></script>
        body {
            font:15px Arial;
        h3 {margin:10px 0;}
        ul {
        li, input {
    <!--THE VIEW.-->
    <div ng-app="myApp" 
            { name:'HP' }, 
            { name:'IBM' }, 
            { name:'LENOVO' }]"

        <div style="float:left;padding:10px;margin:0 auto;">
            <h3>List of Products</h3>
            <ul ng-repeat="products in list">
                <li>{{ }}</li>

        <div style="float:left;margin:30px;">
            <p><input type="number" ng-model="location" placeholder="1" value="1" style="width:80px;" /></p>
            <p><input type="text" ng-model="name" placeholder="Enter an Item" /></p>

            <p><button ng-click="add()">Add Product to List</button></p>
The $Scope
    var myApp = angular.module('myApp', []);

        ['$scope', function ($prod) {

            $prod.add = function () {
                if (angular.isDefined($ && $ != '') {

                    if (angular.isDefined($prod.location)) {

                        // JAVASCRIPT splice() METHOD.
                        $prod.list.splice($prod.location, 0, { name: $ });

                        // CLEAR THE FIELDS.
                        $ = '';
                        $prod.location = "";
        } ]


Also Read: AngularJS “isDefined()” Method

The JavaScript splice() method takes three parameters.

The “splice()” Syntax

array.splice(index, howmany, item1,.....,itemX)

1) index: The first parameter is the index or location where I wish to add my new item. I’ll get the value for the index using $prod.location. That’s the first input box.

2) howmany: The number of items that I need to remove. The value I have assigned in 0, which means it will remove any item from the list.
Note: Add a number more than Zero to see what happens.

3) item: The third parameter is the item. { name: $ }

Well, that’s it. Thanks for reading.

Previous - Implementing jQuery Datepicker inside AngularJS ng-repeat DirectiveNext - Create Charts using Dynamic Data in AngularJS with Angular-Chart.js, jQuery and Web API

Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.

Enter your email id

Delivered by FeedBurner
Tweet this article Facebook Google+

Related Posts:

Join our Google Plus Community and be a part of a discussion!