【问题标题】:angular ng-repeat not working as expected角度 ng-repeat 没有按预期工作
【发布时间】:2015-11-12 12:33:03
【问题描述】:
<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;}

}

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

【问题讨论】:

  • 在视图和控制器中打错 customorscustomers
  • 这不是我想的问题,因为如您所见:.success(function (response) {$scope.customers = response.d;})
  • 返回的数据是否正确?换句话说……如果你中断了成功回调……你有正确的数据吗?
  • 要添加到上面,你不应该也使用HTTP get而不是post吗?
  • 是的responce.d返回[{"Name":"biss","City":"Lebanon"},{"Name":"jiji","City":"America"}]

标签: javascript c# angularjs


【解决方案1】:

用这个替换你的 succes 函数:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2018-01-16
    • 2014-10-14
    • 2017-08-20
    相关资源
    最近更新 更多