最近开始学习angularjs,自己做了在VS2012下AngularJS与服务端的交互的小测试。
1.MVC控制器 代码
T_AdminUser admin = new T_AdminUser();
Model1Container model1contatiner = new Model1Container();
public JsonResult Test()
{
List<T_AdminUser> list_admin = new List<T_AdminUser>();
list_admin = model1contatiner.T_AdminUser.ToList();
JsonResult json = new JsonResult();
json.Data = list_admin;
return Json(json);
}
2.controller.js代码
function ShoppingController($scope, $http) {
$http.post('/Home/Test').success(function (data, status, headers, config) { //注意这里是post提交,若是get会报错
$scope.messages = data.Data;
}
).error(function (data, status, headers, config) {
$(".msg2").html(data);
});
}
$http.post('/Home/Test').success(function (data, status, headers, config) { //注意这里是post提交,若是get会报错
$scope.messages = data.Data;
}
).error(function (data, status, headers, config) {
$(".msg2").html(data);
});
}
3.Index.html部分代码
<div ng-controller="ShoppingController">
<div ng-repeat="msg in messages">
<div>{{msg.ID}}</div>
<div>{{msg.Name}}</div>
</div>
</div>
<div ng-repeat="msg in messages">
<div>{{msg.ID}}</div>
<div>{{msg.Name}}</div>
</div>
</div>