角度ng-repeat不按预期工作

时间:2015-08-19 09:28:53

标签: javascript c# angularjs

<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;}

}

但没有显示结果。我做错了什么?

1 个答案:

答案 0 :(得分:0)

用此替换你的成功函数:

   .success(function (response) {
             $scope.customers = JSON.parse(response.d);
         })

您的对象是字符串格式,如果'Name'是字符串,它将永远无法找到.Name。解析你的回答将解决这个问题。

正如RGraham所说,这并不理想。您希望在服务器端正确处理此问题。