<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循環(huán)-->
<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) {
// 請求失敗執(zhí)行代碼
});
}
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; }
}