Wednesday 5 April 2017

How would you make an Angular service return a promise? Write a code snippet as an example

To add promise functionality to a service, we inject the “$q” dependency in the service, and then use it like so:

angular.factory('testService', function($q){ return { getName: function(){ var deferred = $q.defer(); //API call here that returns data testAPI.getName().then(function(name){ deferred.resolve(name) }) return deferred.promise; } } })

No comments:

Post a Comment