Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my factory service and it is fetching data from a web service

JavaScript
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);


Now, this is being used in my controller

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

The value of service is always undefined or empty. Data is not being passed from factory to controller. Im calling a simple web service, no fancy APIs or WCF.

It there was some static/dummy data, its working fine. The controller is fetching the data and is being displayed.

Where am I doing wrong?

Any help is greatly appreciated.

Thank you

What I have tried:

I'm a newbie to AnuglarJS and was struggling to get the dynamic binding of data but if I put some static/dummy data, it works.

So, where am I messing up? What else is missing in the code.
Posted
Comments
Konstantin A. Magg 27-Aug-16 16:54pm    
Hi RelicV,
as far as I can see it, your factory definition looks fine.

How about your async `GetCustomers` implementation? Are you sure, the http post call returned already, when you set `$scope.Customers`?

Probably a breakpoint at this line could help: `return customer.XMLtoJSON(data);`

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900