Angular ng-repeat没有按预期工作
本文关键字:工作 ng-repeat Angular | 更新日期: 2023-09-27 18:08:35
<body ng-app="myApp">
<div ng-controller="customersCtrl">
<table>
<tr ng-repeat="res in customers">
<td>{{ $index + 1 }}</td>
<td>{{ res.Name }}</td>
<td>{{ res.City }}</td>
</tr>
</table>
</div>
</body>
var myApp = angular.module('myApp', []);
myApp.controller("customersCtrl", function ($scope,$http) {
$http.post('Default.aspx/Getdata', { data: {} })
.success(function (response) {
$scope.customers = response.d;
})
.error(function (data, status, headers, config) {
alert(status+"-------"+data);
});
});
服务器端功能:
[WebMethod]
public static string Getdata()
{
List<customors> people = new List<customors>{
new customors{Name = "biss",City="Lebanon"},
new customors{Name = "jiji",City="America"}
};
JavaScriptSerializer serialiser = new JavaScriptSerializer();
string json = serialiser.Serialize(people);
return json; // [{"Name":"biss","City":"Lebanon"},{"Name":"jiji","City":"America"}]
}
public class customors
{
public string Name{get;set;}
public string City{get;set;}
}
但是没有显示结果。我做错了什么?
将success函数替换为:
.success(function (response) {
$scope.customers = JSON.parse(response.d);
})
您的对象是字符串格式,如果'Name'是字符串,它将永远无法找到.Name
。解析您的响应将修复此问题。
正如格雷厄姆所说,这是不理想的。您希望在服务器端正确地处理这个问题。