【问题标题】:Data not shown after angular http request角度http请求后未显示数据
【发布时间】:2016-05-22 05:47:03
【问题描述】:

我正在使用带有 angular js 的 asp.net Web 服务,返回客户列表 $http 获得成功,但是当我在下面的 html 上迭代它时没有显示数据

<div ng-app="crudModule" ng-controller="CRUD_OperController" name="myForm" novalidate class="my-form">
    <ol data-ng-repeat="cus in Customers">
        <li>{{ cus.CustomerId }} ,{{ cus.CustomerName }},{{  cus.CustomerAddress }}
    </ol>
</div>

有绑定问题吗?

我的脚本在下面

<script  type="text/javascript">
    var app = angular.module('crudModule', []);

    app.service('studentService', function ($http) {
        this.getAllCustomers = function () {
        var response = $http.get("CustomerService.asmx/GetCustomers");
        return response;
        };
    });


    app.controller('CRUD_OperController', function ($scope, studentService) {
        $scope.Customers = [];

        loadData();

        function loadData() {
            var promiseGet = studentService.getAllCustomers();
            promiseGet.then(
                function (pl) {
                    $scope.Customers = pl.data;
                },
                function (errorPl) {
                    $log.error('Some Error in Getting Records.', errorPl);
                }
            );
        }

    }); 

</script>

当写console.info(pl.data);数据如下

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <Customers>
    <CustomerId>1</CustomerId>
    <CustomerName>Hamid</CustomerName>
    <CustomerAddress>Dhaka</CustomerAddress>
  </Customers>
  <Customers>
     <CustomerId>2</CustomerId>
     <CustomerName>Milon</CustomerName>
     <CustomerAddress>Dhaka</CustomerAddress>
  </Customers>
 <Customers>
      <CustomerId>3</CustomerId>
      <CustomerName>Jakir</CustomerName>
      <CustomerAddress>Dhaka</CustomerAddress>
 </Customers>
  <Customers>
     <CustomerId>4</CustomerId>
     <CustomerName>Hamid</CustomerName>
     <CustomerAddress>Khulna</CustomerAddress>
  </Customers>
  </ArrayOfCustomers>

【问题讨论】:

  • 请在帖子中分享$scope.Customers数组的对象。
  • 试试return response.data 而不是response

标签: asp.net angularjs asp.net-mvc asp.net-web-api


【解决方案1】:

Live Demo 给你。

我想推荐 angular-xml 处理 &lt;xml&gt; 角度的响应。

更多细节在angular-xml 和依赖x2js

  1. x2jsangular-xml 包含到您的项目中。

  2. xml 模块注入您的应用程序并配置httpProvider

HTTP 拦截器会将您的所有 XML 响应转换为 JavaScript 对象。

var app = angular.module('crudModule', ['xml']);

app.config(function($httpProvider) {
  $httpProvider.interceptors.push('xmlHttpInterceptor');
});
  1. 请注意您的响应数据结构。

通过console.info(pl),检查从xml解析出来的result对象的嵌套结构

function loadData() {
    var promiseGet = studentService.getAllCustomers();
    promiseGet.then(
      function(pl) {
        console.info(pl);
        $scope.Customers = pl.data.ArrayOfCustomers.Customers;
      },
      function(errorPl) {
        $log.error('Some Error in Getting Records.', errorPl);
      }
    );
  }

【讨论】:

  • 我已经收到控制器中的数据,但无法将其显示到 html 视图中
  • $scope.Customers = pl.data;之后,你会console.info(pl.data);并在这里显示数据吗?
  • 我以为你用的是json,原来是xml,那就试试angular-xml
  • 我在 asmx [WebMethod] 中的代码 public List GetCustomers() { Customers c = new Customers();返回 c.GetCustomerList(); }
猜你喜欢
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多