Skip to content

Instantly share code, notes, and snippets.

@Baudin999
Created November 29, 2013 11:58
Show Gist options
  • Save Baudin999/7704720 to your computer and use it in GitHub Desktop.
Save Baudin999/7704720 to your computer and use it in GitHub Desktop.
Set an async value in AngularJS
// 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