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.

Maciej Szpyra
  • 313
  • 1
  • 12
Gaz Smith
  • 1,100
  • 1
  • 16
  • 30
  • Don't return from async functions try to update whatever you want to do from there. – AurA Apr 07 '17 at 10:40
  • I cant because its an ng-repeat – Gaz Smith Apr 07 '17 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. – deceze Apr 07 '17 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 – Gaz Smith Apr 07 '17 at 10:46

0 Answers0