2016-05-22 142 views
0

我使用有角的js asp.net web服務,返回的客戶名單 $ HTTP GET成功,但數據沒有顯示時,我重複它的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> 
+0

請共享'在後$ scope.Customers'陣列的對象。 – Saad

+0

嘗試'return response.data'而不是'response'。 – Harish

回答

0

Live Demo爲您服務。

我想推薦angular-xml來處理<xml>角度響應。

更多詳細信息在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'); 
}); 
  • 小心你的響應數據結構。
  • 通過console.info(PL),檢查結果對象的從XML解析的嵌套結構

    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); 
         } 
        ); 
        } 
    
    +0

    我已收到控制器中的數據,但無法將其顯示到html視圖 – Belayet

    +0

    '$ scope.Customers = pl.data;'後,你會'console.info(pl.data);'並在這裏顯示數據? –

    +0

    是的,它顯示數據爲arrayofCustomers – Belayet