I have a situation where I need to modify the result of a promise returned by a function in one service and make this altered value accessible to my controllers from another service.
angular.module('test').service("service1", function($q) {
function doSomething(something) {
//parses some data
return deferred.promise;
}
return {
doSomething: doSomething
}
});
angular.module('test').service("service2", ['service1', $q, function(service1, $q) {
var deferred = $q.defer();
var myResult = deferred.promise;
service1.doSomething("test").then(function (result) {
var alteredResult = result + "Altered"; //result I want to be accessible by my controllers
deferred.resolve(alteredResult)
});
return {
myResult: myResult
}
}]);
angular.module('test').controller('testController', ['$scope', 'service2', function ($scope, service2) {
$scope.test = service2.myResult;
}]);
I encountered an issue when trying to initialize and update myResult
as it was passed to the controller before being altered. This led me to explore creating another promise within service2
.
However, the result in $scope.test
ended up being an empty {}
javascript object.
How can I correctly store the modified result of service2
for access by controllers?
Thank you for your time. Please let me know if you require more information or if I am unclear.