In the past, I encountered a challenge with the $http service which I managed to solve by creating a dedicated service for handling my requests. However, as my requests grew larger, this solution started to seem inefficient. Instead of just assigning a simple word from the response to a $scope value, I now need to store an entire JSON configuration in the $scope.
Although I grasp the concept of promises and understand that the value returned by $http is initially null until the response arrives, the configuration I am working with is not extensive, yet I never seem to receive a response. Even when I display the variable holding the value on the view, it remains unchanged when the function is triggered.
Here is the code:
View
...
<div>
{{loadedConfig}}
</div>
<button ng-click="loadconfig()">load</button>
Controller
app.controller('ConfigurationCtrl', function ($scope, $rootScope, configloader) {
...
$scope.loadedConfig = 'noconfig'
$scope.loadconfig = function() {
configloader.load().then(function (response) {
$scope.loadedConfig = response.data;
});
};
Factory
angular
.module('app')
.factory('configloader', configloaderFactory);
function configloaderFactory($http) {
var service = {
load: load
}
return service;
function load() {
return $http.get('url/config');
}
}
When I try making a simpler request, everything works fine. I've experimented with various other methods like $q.deferred, success()/error(), or even implementing a custom sleep() function but nothing seems to work. Is the code inside the 'then' supposed to execute once I receive the response? (I assume so, but it never does).
Thanks