2016-08-12 101 views
0

這是我廠的服務,它是從Web服務AngularJS工廠服務不返回動態數據控制器

var customersFactory = function ($http) { 
    var customer = []; 
    var getCustomersURL = "http://localhost:50340/Services/CustomerService.asmx/GetCustomers"; 

    customer.XMLtoJSON = function (data) { 
     data = data.replace('<?xml version="1.0" encoding="utf-8"?>', ''); ; 
     data = data.replace('<string xmlns="http://tempuri.org/">', '').replace('</string>', ''); 
     return $.parseJSON(data); 
    }; 

    customer.GetCustomers = function() { 
     $http.post(getCustomersURL).success(function (data, status, headers, config) { 
      return customer.XMLtoJSON(data); 
     }).error(function (ata, status, headers, config) { }); 
    }; 

    return customer; 
}; 

app.factory('customersFactory', customersFactory); 

現在獲取數據,這是被我的控制器使用

app.controller("CustomersController", function ($scope, customersFactory) { 
    var service = []; 
    service = customersFactory.GetCustomers(); 
    $scope.Customers = service; 

    if ((typeof (service) != 'undefined') && service.length > 0) { 
     service.then(function (result) { 
      $scope.Customers = result; 
     }); 
    } 
}); 

的服務的價值總是未定義或爲空。數據沒有從工廠傳遞到控制器。我調用一個簡單的Web服務,沒有花哨的API或WCF。

它有一些靜態/虛擬數據,它的工作正常。控制器正在讀取數據並正在顯示。

我在哪裏做錯了?

任何幫助,非常感謝。

謝謝

回答

1

改變這一行var customer = [];這個var customer = {};

或者更好的是使之類......

改變這個返回承諾:

customer.GetCustomers = function() { 
     return $http.post(getCustomersURL).error(function (data, status, headers, config) { }); 
    }; 

和在控制器中的使用:

app.controller("CustomersController", function($scope, customersFactory) { 
    $scope.Customers = []; 

    customersFactory.getCustomers().success(function(data, status, headers, config) { 
    $scope.Customers = customersFactory.XMLtoJSON(data); 
    }); 
}); 
+0

非常感謝WalksAway。收到。我明白我出錯的地方。非常感謝。 – Vamsi

相關問題