AngularJS Variable in service is not updating in view?

前端 未结 1 901
心在旅途
心在旅途 2021-01-15 22:57

I have spent a couple of days banging my head against the table, reading blog posts, and SO questions around my issue. I tried several variations of the code I have below an

1条回答
  •  执笔经年
    2021-01-15 23:18

    Return the $http promise from the service:

    app.service("API", function($http) {
        this.getTasks = function() {
            return $http.get(url);
        };
    });
    

    Then in the controller, extract the data from the promise:

    app.controller("mainController", function($scope, API) {
        this.$onInit = function () {
            console.log("Initializing app");
            API.getTasks().then(function(response) {
                $scope.tasks = response.data;
            });    
        };
    });
    

    For more information, see AngularJS $http Service API Reference.

    0 讨论(0)
提交回复
热议问题