I would like to load 2 json data in $scope inside my controller before doing something like a basic case. But $http inside my factory return the state object because it returns a promise.
My Provider as factory object
(function(angular) {
'use strict';
angular.module('myModule').factory("config", function($http) {
return{
getConfig: function () {
return $http.get('json/config.json').then(function(response) {
return response.data;
});
},
getPreferences: function () {
return $http.get('json/preferences.json').then(function(response) {
return response.data;
});
}
};
});
})(window.angular);
How to store all my external json data from several files in $scope variables inside my Main Controller without timeout or add several nested promise.then ? Actualy, I would like to know if there is a way to store all json data before the controller loading ?
(function(angular) {
'use strict';
angular.module('myModule')
.controller('MainCtrl', ['$scope', 'config', function ($scope, config, ){
$scope.config = ?????????? ;
$scope.preferences = ???????????? ;
}]);
})(window.angular);
Just call your methods from your service
config
in your controller ...In case that you want this configuration data before the initializaation of the controller and you are using router, you can resolve it from the router and it will be available when your controller is triggered.
Router
Controller
Edited