Angularjs的ng-repeat是用來循環產生呈現數據。
當我們需要在ng-repeat循環中呈現一系列Checkbox時,某些checkbox選項是默認選中的。
在ASP.NET MVC程序中的Entity,准備一些數據:

public IEnumerable<Car> Cars() { return new List<Car>() { {new Car() { ID = 1, Name = "瑪莎拉蒂",Selected=false }}, {new Car() { ID = 2, Name = "奔馳" ,Selected=false }}, {new Car() { ID = 3, Name = "寶馬" ,Selected=true }}, {new Car() { ID = 4, Name = "保時捷",Selected=false }} }; }
在ASP.NET MVC的控制器中,准備一個方法。這個方法是讀取Entity的數據,並為angularjs准備一個呼叫的方法:

public JsonResult GetCars() { CarEntity ce = new CarEntity(); var model = ce.Cars(); return Json(model, JsonRequestBehavior.AllowGet); } public ActionResult CheckBox_IsChecked() { return View(); }
OK,下面我們開始我們真正的程序angularjs:
Html程序:

<div ng-app="PilotApp" ng-controller="CarCtrl"> <div ng-repeat="c in Cars"> <div> <input type="checkbox" value="{{c.ID}}" ng-checked="{{c.Selected}}" />{{c.Name}} </div> </div> </div>
Angularjs程序:

var pilotApp = angular.module("PilotApp", []); pilotApp.controller('CarCtrl', function ($scope, $http) { var obj = {}; $http({ method: 'POST', url: '/Car/GetCars', dataType: 'json', headers: { 'Content-Type': 'application/json; charset=utf-8' }, data: JSON.stringify(obj), }).then(function (response) { $scope.Cars = response.data; }); });
程序運行最終呈現的效果: