Created
November 29, 2013 11:58
-
-
Save Baudin999/7704720 to your computer and use it in GitHub Desktop.
Set an async value in AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// imagine a factory with the following code somewhere in the factory: | |
app.factory('asyncFactory', function($q, $timeout) { | |
// a factory always needs to return a value | |
return { | |
getAsyncValue: function () { | |
var deferred = $q.defer(); | |
$timeout(function () { | |
deferred.resolve('This is an item'); | |
}, 2000); | |
return deferred.promise; | |
} | |
} | |
}); | |
// now inside some controller we want to consume this factory: | |
$scope.description = simpleFactory.getAsyncValue(); | |
$scope.description.then(function (result) { | |
$scope.description = result; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment