0

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?

Vaethin
  • 316
  • 4
  • 18
  • You need to understand the basics of how asynchronous calls work in javascript and then how `promise` works. Read the duplicate, the answer is very well written. – Igor Oct 05 '17 at 10:58
  • You're looking for the `ES2015+: Promises with then()` part of the answer on that question. – EpicPandaForce Oct 05 '17 at 11:07
  • See also https://docs.angularjs.org/api/ng/service/$q for AngularJS promises – EpicPandaForce Oct 05 '17 at 11:09
  • Thanks for the link, has been helpful already. – Vaethin Oct 05 '17 at 11:30
  • What do you think you're *returning to*? You're already returning a promise from `GetPhones` which is assigned to `phones`; where is the second `return` inside the callback going to go…? – deceze Oct 05 '17 at 12:28
  • See update @Igor – Vaethin Oct 05 '17 at 12:33
  • @deceze That is one of the things I'm wondering. I tried using only one return as well though, same result. – Vaethin Oct 05 '17 at 12:34
  • Well, that's the answer: it doesn't go anywhere. – deceze Oct 05 '17 at 12:49
  • @deceze : How would I cause it to go where I want it to go? Is assignement through $scope.phones = response.data the only way ? – Vaethin Oct 05 '17 at 12:57
  • Yes, the only way. – deceze Oct 05 '17 at 12:59
  • @deceze : Ok, so what would I do if this happens in a component for example? `angular.module('phonecatApp'). component('phoneList', { templateUrl : template.html, controller : function PhoneListController($http, $location){ this.GetPhones = function(){ $http.get(url).then(function(response{ this.phones = response.data;}); (...)` That doesn't work, because the this refers to the function, not the controller. $ctrl.phones and $scope.phones (adding $scope above) don't work either. It feels like I need a return value to be able to get that data or is there another way? – Vaethin Oct 05 '17 at 13:51
  • → https://stackoverflow.com/q/20279484/476 – deceze Oct 05 '17 at 14:13

0 Answers0