Created
September 16, 2014 08:26
-
-
Save janbaer/8c2c7f7cc2bf78cdbe5e to your computer and use it in GitHub Desktop.
Angular promises
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body ng-app="MyApp"> | |
<div ng-controller="MyController"> | |
<button ng-click="fetchData()">Fetch data</button> | |
<p> | |
<div>{{ result }}</div> | |
</p> | |
<p><span style="color:red;">{{errorMessage}}</span></p> | |
</div> | |
<script id="jsbin-javascript"> | |
var app = angular.module('MyApp', []); | |
app.factory('MyService', function($q, $timeout) { | |
var fetchData = function () { | |
var deferred = $q.defer(); | |
$timeout(function() { | |
deferred.reject(new Error('Error while fetching data')); | |
}, 1000); | |
return deferred.promise; | |
}; | |
return { | |
fetchData: fetchData | |
}; | |
}); | |
app.controller('MyController', function($scope, MyService) { | |
$scope.result = "Hello"; | |
$scope.fetchData = function () { | |
MyService.fetchData() | |
.then(function(data) { | |
$scope.result = data; | |
}, function(error) { | |
$scope.errorMessage = error.message; | |
}); | |
}; | |
}); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"><\/script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body ng-app="MyApp"> | |
<div ng-controller="MyController"> | |
<button ng-click="fetchData()">Fetch data</button> | |
<p> | |
<div>{{ result }}</div> | |
</p> | |
<p><span style="color:red;">{{errorMessage}}</span></p> | |
</div> | |
</body> | |
</html></script> | |
<script id="jsbin-source-javascript" type="text/javascript">var app = angular.module('MyApp', []); | |
app.factory('MyService', function($q, $timeout) { | |
var fetchData = function () { | |
var deferred = $q.defer(); | |
$timeout(function() { | |
deferred.reject(new Error('Error while fetching data')); | |
}, 1000); | |
return deferred.promise; | |
}; | |
return { | |
fetchData: fetchData | |
}; | |
}); | |
app.controller('MyController', function($scope, MyService) { | |
$scope.result = "Hello"; | |
$scope.fetchData = function () { | |
MyService.fetchData() | |
.then(function(data) { | |
$scope.result = data; | |
}, function(error) { | |
$scope.errorMessage = error.message; | |
}); | |
}; | |
});</script></body> | |
</html> |
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
var app = angular.module('MyApp', []); | |
app.factory('MyService', function($q, $timeout) { | |
var fetchData = function () { | |
var deferred = $q.defer(); | |
$timeout(function() { | |
deferred.reject(new Error('Error while fetching data')); | |
}, 1000); | |
return deferred.promise; | |
}; | |
return { | |
fetchData: fetchData | |
}; | |
}); | |
app.controller('MyController', function($scope, MyService) { | |
$scope.result = "Hello"; | |
$scope.fetchData = function () { | |
MyService.fetchData() | |
.then(function(data) { | |
$scope.result = data; | |
}, function(error) { | |
$scope.errorMessage = error.message; | |
}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment