2015-11-22 51 views
0

如何可以在角排至第一添加行到第一表中的angularjs

HTML:

<title>Add Rows</title> 

    <link href="http://cdn.foundation5.zurb.com/foundation.css" rel="stylesheet"> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> 
    </head> 

    <body ng-controller="MainController"> 

    <a href="#" class="button" ng-click="addRow()">Add Row {{counter}}</a> 

<table> 
    <thead> 
    <tr> 
     <th width="200">Some Header</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr ng-repeat="rowContent in rows"> 
     <td>{{rowContent}}</td> 
    </tr> 
    </tbody> 
</table>  

    </body> 
</html> 

JS:

angular.module('MyApp', []) 
.controller('MainController', [ '$scope', function($scope) { 

    $scope.rows = ['Row 1', 'Row 2']; 

    $scope.counter = 3; 

    $scope.addRow = function() { 

    $scope.rows.push('Row ' + $scope.counter); 
    $scope.counter++; 
    } 
}]); 

http://codepen.io/calendee/pen/buCHf

在下面的例子中 它在最後添加了行,我怎麼能成爲第一行而不是最後一行。

回答

2

隊友,只使用不印字,而不是推,然後它會插入到陣列的頂部,

$scope.rows.unshift('Row ' + $scope.counter); 
    $scope.counter++; 

做了搗鼓你:

http://codepen.io/anon/pen/QjPxvN?editors=101

+0

感謝很多做工精細 – Asad

相關問題