I have created angularJs factory service to dealing with REST calls.Service is worked fine.I have some cases that i need to set values in to $scope.variable and access them in outside of resource service.But i got undefined. I wrapped them inside angularJs $q
but seems like i did some mistake.please help me to solve this.
AngularJs Factory
myApp.factory('MyService',function($resource) {
return{
GetMYData:$resource(my rest service URL, {});
}
});
In Controller
$scope.myMap={};
$scope.promises=[];
$scope.myData = MyService.GetMYData.query();
$scope.myData.$promise.then(function (result) {
result.forEach(function(e, i) {
//Do something
var deferred = $q.defer();
$scope.myMap[e.key]=e; //Put elements into map and i tried to access map from outside service by giving key
deferred.resolve(result);
$scope.promises.push(deferred.promise);
$scope.myMap[$location.search()['dataID']] //This display the actual value
})
...
}
If URL parameter present in the URL i do the following things.
if($location.search()['dataID']){
$q.all($scope.promises).then(
function(data) {
console.log($scope.myMap[$location.search()['dataID']]) // throws undefined
},function(response) {
//Handle if promises are rejected
})
}
Please let me know how can i access the map values set by angularJs resource service from outside of service.