In my $routeProvider I have a resolve property to get data from an API before I instantiate my controller. Why is it when I use the $q with the $http service, I can pass the data to my controller. Aren't they both doing the same thing?
Here is my original approach where the data wasn't able to get passed to the controller:
AccountService.js
app.factory('AccountService', ['$http', '$q', function ($http, $q) {
return {
GetAccounts: function () {
return $http.get('api/Account/GetAccounts')
.then(function success(response) {
return response;
}, function error(response) {
̶r̶e̶t̶u̶r̶n̶ throw console.log("Oops!");
});
},
};
}]);
To pass the data into the controller I change up the GetAccounts to this:
app.factory('AccountService', ['$http', '$q', function ($http, $q) {
return {
GetAccounts: function () {
var deferred = $q.defer();
$http.get('api/Account/GetAccounts')
.then(function (data) {
deferred.resolve(data.data);
})
.catch(function (response) {
deferred.reject(response);
})
return deferred.promise;
},
};
}]);
route.js
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/users', {
template: '<admin accounts="$resolve.accounts"></admin>',
resolve: {
accounts: function (AccountService) {
return AccountService.GetAccounts()
.then(function (data) {
return data;
})
}
}
})
}]);