Right now, I want to be able to set some data for a service, and to be able to get the data on a second controller. I saw on my debugger, that the code isn't working correctly because my logic isn't asynchronous.
myApp.factory('Data', function($http) {
var storage;
this.callMe = function() {
return $http.get("https://api.github.com");
};
this.getStorage = function() {
return storage;
};
this.setStorage = function(v) {
storage = v;
};
return this;
});
function FirstCtrl($scope, Data, $q) {
Data.callMe().then(function(response) {
$scope.data = response.data;
});
Data.setStorage($scope.data);
}
function SecondCtrl($scope, Data) {
var data = Data.getStorage();
}
I have a plnkr: http://plnkr.co/edit/0FicRx9FVJfeWFZhO4Ya?p=streamer
I tried using $q.defer, but I wasn't able to get the effect I wanted.