Unable to get data from $ q to region

I am trying to bind some data returned from the API to my area using promises using $ q, I can retrieve data from the server without any problems (I see that JSON is returned using a script) however the $ scope variable remains empty. Any help would be greatly appreciated! Thanks in advance.

code:

toDoListService.js

 app.factory("toDoListService", function ($http, $q) {
        var deferred = $q.defer();

        return {
            get: function () {

                $http({ method: 'GET', url: '/api/todo/' }).
                    success(function (data) {
                        deferred.resolve(data);
                    }).
                    error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                return deferred.promise;
            }
});

toDoListController.js

app.controller("toDoListController", function($scope, toDoListService){
      $scope.toDoList = toDoListService.get();
});
+4
source share
3 answers

First of all, you have to put var deferred = $q.defer();in your function getso that everyone has gettheir own pending object.

-, get, . :

app.controller("toDoListController", function($scope, toDoListService){
    toDoListService.get().then(function(data){
           $scope.toDoList = data;
    });
});
+3

$scope.toDoList . , , , , , 1.2.

, :

app.controller("toDoListController", function($scope, toDoListService){
  toDoListService.get().then(function(data){
    $scope.toDoList = data;
  });
});

, $q , $http . :

app.factory("toDoListService", function ($http){       
   return {
     get: function () {
        return $http({ method: 'GET', url: '/api/todo/' });
     }
   };
});
+3

, :

toDoListService.js

app.factory("toDoListService", function ($http, $q) {
    return {
        get: function () {
            return $http({ method: 'GET', url: '/api/todo/' });
        }
    }
});

toDoListController.js

app.controller("toDoListController", function($scope, toDoListService) {
    toDoListService.get().then(function(response){
        $scope.toDoList = response.data;
        return response;
    });
});

response , promises .

+2

Source: https://habr.com/ru/post/1527199/


All Articles