Edit: Thanks for the duplicate link, it has been helpful, but I'm still confused. I understand that it wouldn't work if I just wrote $scope.phones = function(){ return $http.get(url)}
which is what the original op did. I am already using .then to evaluate the promise. I don't understand why I can't get the value the promise returns into my $scope.phones without using $scope.phones = response.data in my promise.
if I write $scope.phones = GetPhones() basically what it says is "Call function GetPhones, evaluate it and return the promise". But I'm also writing .then return response.data
. Why isn't that enough to evaluate the promise?
I understand that it's async so the rest of the code has already run, but that should include the ng-repeat I use in my template. Why does changing the value through assignining response.data to it cause my Browser to load the data but assigning return response.data to it, doesn't trigger an update?
I am trying to query an API to get some data and display it in a list on my .html file. However for some reason my "return" loses the data... (see commented code)
(I am doing a variation of the official angularjs tutorial)
$scope.GetPhones = function () {
return $http.get($scope.baseurl + '/api/PhoneApi')
.then(function (response) {
$scope.phones = response.data; //It works like this
return response.data; //but not like this
});
};
$scope.phones = GetPhones();
Even when both lines are in the code it'll still work, it's like GetPhones(); in $scope.phones is never called but the function is called once, when I load the page ...
I suppose I am doing something wrong when I pass my .then return value to the function return value at the start? Hard to find an example of this actually, most people seem to use $scope.var = response.data;
.
Help me understand this?