0

I'm trying to return something from an angular function to the html but from the success function of the http request. In this case, I'm using Google API to get the miles, then I want to return it to html but it won't return unless it's in the main function.

This is the code in the controller :

$scope.getDistance=function(pcodefrom,pcodeto,id){

if($scope.milesGot.indexOf(id) == -1) { //if not already done this request once

    $scope.milesGot.push(id);

var url = 'mylinktogooglematrixapiishere';

 $http({method: 'GET', url: url}).success(function(data) {
                   var to = data.destination_addresses;
                   var from = data.destination_addresses;

                   var miles = data.rows[0].elements[0].distance.text;
                    return miles;

                }).error(function(data){

                });

        } 

    }

The 'miles' variable is fine, even when I change it to plain text like "test" it won't return.

4
  • Don't return from async functions try to update whatever you want to do from there. Commented Apr 7, 2017 at 10:40
  • I cant because its an ng-repeat Commented Apr 7, 2017 at 10:40
  • What are you doing with an asynchronous function in an ngRepeat? You should probably populate your async data before in a service or controller before you try to ngRepeat it. Commented Apr 7, 2017 at 10:43
  • I use ng-map to show a bunch of maps then i have {{functionname(pcodeto,pcodefrom,id)}} and in the function i push the ids into an array and check its not done the check for that id so it only runs the http request once for each map Commented Apr 7, 2017 at 10:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.