0

Reasonably new to Angular. I have a service which purpose is to get information from a JSON file, iterate through each object, so I can access the data from the controller. I can access the JSON data from the $http request, but not the JSON data from the for loop within the service.

Is this is the best way to loop through items in AngularJS, if so, how do I access and display this information from the controller?

app.factory('fxRate', ['$http', '$q', function($http, $q){

var factory = {};

factory.getFx = function() {
    var deferred = $q.defer();

    $http.get('../json/mcfx.json')
        .then(
            function(response){

                var d, i;

                var rateData = response.data.fxrate;

                for (var i in rateData) {
                    var fx = rateData[i].usdToEur;
                    var fxDate = rateData[i].date;
                }
                deferred.resolve(response.data);
            }, 
            function(errResponse){
                deferred.reject(errResponse.data);
            }
        )

    return deferred.promise;

}

return factory;

}]);

app.controller('dashboard', ['$scope', 'fxRate', function($scope, fxRate){ 

$scope.dailyFx = function() {
    fxRate.getFx().then(function(data){
        console.log(data)
    });
}

$scope.dailyFx();

}])

1 Answer 1

1

Make it simple., return promise from factory\service and resolve in controller. Like below:

Factory

app.factory('fxRate', ['$http', function($http){
var factory = {};
factory.getFx = function() {
    return $http.get('../json/mcfx.json');
}

return factory;
}]);

Controller

app.controller('dashboard', ['$scope', 'fxRate', function($scope, fxRate){ 
$scope.dailyFx = function() {
    fxRate.getFx().then(function(resonse) {
        console.log(resonse.data)
        //handle resonse\data, or assign to some $scope property.
        var rateData = response.data.fxrate;
        for (var i in rateData) {
           var fx = rateData[i].usdToEur;
           var fxDate = rateData[i].date;
         }
    });
}

$scope.dailyFx();
}])

To show this data on view\html, you need to assign this to some $scope property, like $scope.fxRateData = response.data.fxrate, and then in html render it the way you want.

For example only :

<div ng-repeat="fxrate in fxRateData track by $index">
  <span>{{fxrate.usdToEur}}</span>
  <span>{{fxrate.date}}</span>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

This is true, though if I wanted to keep functions separate from the controller....... how would I go about that?
@PatrickMcDermott : your function $scope.dailyFx() is quite separate and further you can extracta nother method from response section, if you do some manipulation with response.data., like some handleResponse(response.data)

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.