AngularJS: Push to first or 0 index in $scope array

by Raul Corona Xolaltenco

HTML

<div ng-controller="Ctrl">
    <pre>{{data}}</pre><br/><br/>
    <input type="button" ng-click="addItem()" value="Add Item" /><br/>
    <i>Should add the item to the first index or 0 index.</i>
</div>

CSS

input{
    padding:5px;
}

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);
    };
}