AngularJS table循環數據


<div ng-app="CycleTableApp" ng-controller="CycleTableContrl as vm">
    <h1>類別列表</h1>
    <table class="table">
        <thead>
            <tr>
                <th>類別編號</th>
                <th>類別名稱</th>
            </tr>
        </thead>
        <tbody>
            <!--ng-repeat指令,類似foreach循環-->
            <tr ng-repeat="item in vm.firstSortList">
                <td>
                    <p>{{ item.id}}</p>
                </td>
                <td>
                    <p>{{item.displayName}}</p>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<script src="~/Scripts/angular.min.js"></script>
<script>
    var app = angular.module('CycleTableApp', []);
    app.controller('CycleTableContrl', function ($scope,$http) {
        var vm = this;
        vm.getdata = function () {
            $http({
                method: 'POST',
                url: '/AngularjsStudy/GetFirstSort',
                data: {}
            }).then(function successCallback(data) {
                //data有多余屬性,data.data才是controller返回的data
                vm.firstSortList = data.data;
            }, function errorCallback(response) {
                // 請求失敗執行代碼
            });
        }
        vm.getdata();
    });
</script>

Controller

 public ActionResult GetFirstSort()
{
    List<FirstSort> firstSorts = new List<FirstSort>();
    firstSorts.Add(new FirstSort() {id=1,displayName= "綁定分類1" });
    firstSorts.Add(new FirstSort() { id = 2, displayName = "綁定分類2" });
    firstSorts.Add(new FirstSort() { id = 3, displayName = "綁定分類3" });
    firstSorts.Add(new FirstSort() { id = 4, displayName = "綁定分類4" });
    firstSorts.Add(new FirstSort() { id = 5, displayName = "綁定分類5" });
    return Json(firstSorts);
}

實體類

public class FirstSort
{
    public int id { get; set; }
    public string displayName { get; set; }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM