0

I've got this code...

Agencyapp.factory('AgencyData', function ($http, $log) {
return {
    getAgencies: function(successcb){
        $http({ method: 'GET', url: 'http://localhost/MappingServicesWebAPI/api/mapping' }).
        success(function(data){
            successcb(data);
        }).
        error(function(data){
            $log.warn(data, status, headers, config);
        })
    }

}
});

Which gets data from a WebAPI. The $scope.Agencies model gets populated with an AgencyList array. When I try to use that array...

<div ng-controller="AgenciesCtrl">
    <select ng-model="Agencies">
        <option>Select Agency</option>
        <option ng-repeat="A in Agencies" >{{A.AgencyList.AgencyName}}</option>
    </select>
    {{Agencies.AgencyList}}
</div>

It's empty...can someone help me with what I might be doing wrong?

Here's the controller, sorry I thought I included it...

Agencyapp.controller('AgenciesCtrl', function AgenciesCtrl($scope, AgencyData) {

AgencyData.getAgencies().then(function (rtnAgencies) {
    $scope.Agencies = rtnAgencies;

});

});

I tried to post a picture of the populated $scope object but I don't have enough reputation points...

It Looks like this(each indent is a nested object)...

$scope.Agencies
  [prototype]
  AgencyList[]
    [0]
      [prototype]
      AgencyID    -10168
      AgencyName  "#1 Insurance Agency"

If I hard code data...

function AgenciesCtrl($scope, AgencyData) {

$scope.Agencies = [
            {
                AgencyID: 'Test One',
                AgencyName: 'Agency Test 1'
            },
            {
                AgencyID: 'Test Two',
                AgencyName: 'Agency Test 2'
            }];


};

It Works

If I hard code data inside the function call

function AgenciesCtrl($scope, AgencyData) {



AgencyData.getAgencies().then(function (rtnAgencies) {
    $scope.Agencies = [
            {
                AgencyID: 'Test One',
                AgencyName: 'Agency Test 1'
            },
            {
                AgencyID: 'Test Two',
                AgencyName: 'Agency Test 2'
            }];


});


};

It doesn't work

I haven't seen one good example of using data from a web api...all examples I have seen hard code data, what's the point in that?

4
  • Can you post your controller? It may be that a $digest cycle never kicked off. Commented Oct 7, 2013 at 18:23
  • I suppose you handle data passed to successcb wrong, but I can't say anything without controller. Commented Oct 7, 2013 at 18:58
  • Anyone have any ideas for me? Commented Oct 8, 2013 at 14:29
  • Any ideas...how do you get a question answered on this site? Commented Oct 9, 2013 at 11:49

2 Answers 2

1

First, remove ng-model="Agencies" from your <select> element. You dont want to bind your select element to the same object that you are running an ng-repeat on. Try something like <select ng-model="selectedAgency" > instead.

Second, I suggest utilizing angular's deferred API to return a promise, which will then be resolved with the value of the data returned from the server, when it is finished:

Agencyapp.factory('AgencyData', function ($http, $log, $q) {
return {
    getAgencies: function(successcb){
        var deferred = $q.defer();
        $http({ method: 'GET', url: 'http://localhost/MappingServicesWebAPI/api/mapping' }).
        success(function(data){
            deferred.resolve(successcb(data)); //I dont know what successcb() does                
        }).
        error(function(data){
            deferred.reject(data);
            $log.warn(data, status, headers, config);
        })
        return deferred.promise;
    }
};
});

In your controller, you would then do something like this:

  AgencyData.getAgencies().then(function(data) {
     $scope.Agencies = data;
  });

As soon as the getAgencies() function finishes getting the data, the $scope.Agencies object will be updated with the resulting data.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response...I have implemented the changes you suggested but get the same result a populated $scope object that doesn't seem to carry back to the page.
Anybody have any ideas for me?
what does successcb do? have you been able to confirm through firebug that the data is indeed being successfully returned in the format you are expecting? What format is the data being returned in, can you provide a screenshot or sample of what the actual returned data looks like?
I provided that by editing my original post...I tried to post a picture but I don't have enough reputation points. I took the cbSuccess function out and I get the same results...it was just a call back function.
Any ideas...how do you get a question answered on this site?
0

It turns out I had my...

<div ng-controller="AgenciesCtrl">
<select ng-model="Agencies">
    <option>Select Agency</option>
    <option ng-repeat="A in Agencies" >{{A.AgencyList.AgencyName}}</option>
</select>
{{Agencies.AgencyList}}
</div>

Inside a that was controlled by a Javascript Library called Slidebox.js. Once I removed Slidebox.js everything worked correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.