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


please me implement function. have array of items in $scope. now, when click on add item button, want push new item first index or 0 index of array. in advance. :)

here's working jsfiddle 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);     }; } 

solved problem using splice() instead of push() , assigning array index 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 updated fiddle this. http://jsfiddle.net/limeric29/xvhne/


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -