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

All we need is an easy explanation of the problem, so here it is.

Please help me implement this function. I have an array of items in my $scope. Now, when I click on the Add Item button, I want to push a new item to the first index or 0 index of that array. Thanks in advance. 🙂

Here’s a working jsFiddle to start with: http://jsfiddle.net/limeric29/7FH2e/

HTML:

<div ng-controller="Ctrl">
    {{data}}<br/>
    <input type="button" ng-click="addItem()" value="Add Item" />
</div>

JavaScript:

function Ctrl($scope) {
    $scope.data = [
    new String('Item 5'), new String('Item 4'), new String('Item 3'), new String('Item 2'), new String('Item 1')];

    $scope.addItem = function () {
        var c = $scope.data.length + 1;
        var item = new String('Item ' + c)
        $scope.data.push(item);
    };
}

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can use the unshift function.

function Ctrl($scope) {
$scope.data = [
new String('Item 5'), new String('Item 4'), new String('Item 3'), new String('Item 2'), new String('Item 1')];

$scope.addItem = function () {
    var item = new String('Item ' + c)
    $scope.data.unshift(item);
};
}

Method 2

Solved my problem by using splice() instead of push() and assigning to what array index to insert.

HTML:

<div ng-controller="Ctrl">
    <pre>{{data}}</pre><br/>
    <input type="button" ng-click="addItem()" value="Add Item" />
</div>

Javascript:

function Ctrl($scope) {
    $scope.data = [
    new String('Item 4'), new String('Item 3'), new String('Item 2'), new String('Item 1')];

    $scope.addItem = function () {
        var c = $scope.data.length + 1;
        var item = new String('Item ' + c)
        $scope.data.splice(0, 0, item);
    };
}

Here’s the updated fiddle for this. http://jsfiddle.net/limeric29/xvHNe/

Method 3

$scope.data.unshift(item);

One line, not sure why the others made it so difficult

Method 4

Try this:

function Ctrl($scope) {
    $scope.data = [
    new String('Item 4'), new String('Item 3'), new String('Item 2'), new String('Item 1')];

    $scope.addItem = function () {
        var c = $scope.data.length + 1;
        var item = new String('Item ' + c);
        $scope.data.push(item);
    };
}

Method 5

I think, not necessary this operation. You can solve it like this;

<div ng-controller="Ctrl">
    <!-- "$index" is short parameter, "true" statment is reverse parameter -->
    {{data | reverse:$index:true}}<br/>
    <input type="button" ng-click="addItem()" value="Add Item" />
</div>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply