I've developed a provider
called MyRestServices
to handle my REST-Services:
app.provider('MyRestServices', function() {
this.baseUrl = null;
this.setBaseUrl = function(_baseUrl) {
this.baseUrl = _baseUrl;
};
this.$get = ['$http', function($http) {
var _baseUrl = this.baseUrl;
function getMyData() {
return $http.get(_baseUrl + 'data1/?token=' + token + '&key=' + key);
}
function preGetTokenAndKey() {
return $http.get(_baseUrl + 'keyAndToken/');
}
return {
getMyData: getMyData,
preGetTokenAndKey: preGetTokenAndKey
};
}];
});
Prior to making any REST service calls, I set up the configuration.
app.config(function(MyRestServicesProvider) {
MyRestServicesProvider.setBaseUrl('https://www.test.com/rest/');
});
In addition, there's a controller named HeadCtrl
that needs to execute preGetTokenAndKey
first in order to retrieve both the key
and token
required for subsequent REST API calls like getMyData
.
app.controller('HeadCtrl', function (MyRestServices) {
MyRestServices.preGetTokenAndKey().success(function(data) {
var key = data.dataSection.key;
var token = data.dataSection.token;
});
});
The issue arises when I try to call getMyData
from another controller without having access to both the key
and token
.
Therefore, the solution involves waiting for the successful execution of preGetTokenAndKey
first before passing these values to the MyRestServices
provider.
How can I effectively tackle these challenges?