Skip to content

Instantly share code, notes, and snippets.

@cef62
Created February 20, 2015 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cef62/f232751f3e832f316fb2 to your computer and use it in GitHub Desktop.
Save cef62/f232751f3e832f316fb2 to your computer and use it in GitHub Desktop.
Matteo Playground Example of promise with services in angularJs // source https://jsbin.com/reluge
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Example of promise with services in angularJs">
<script src="https://rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<meta charset="utf-8">
<title>Matteo Playground</title>
</head>
<body ng-app="app">
<div ng-controller="MyCtrl as ctrl">
<h2>Current result: {{ctrl.result}}</h2>
</div>
<script id="jsbin-javascript">
(function() {
'use strict';
angular.module('app', [])
.service('myRemoteService', MyRemoteService)
.controller('MyCtrl', MyCtrl);
function MyRemoteService($timeout, $q) {
this.defaultValue = 'default';
this.getUser = function getUser() {
return $timeout(function(){
return 'completato';
}, 3000)
.then(function(res){
return $timeout(function(){
return res + ' 2nd call';
}, 3000);
});
};
}
function MyCtrl(myRemoteService) {
var self = this;
this.result = myRemoteService.defaultValue;
myRemoteService.getUser()
.then(function resultHandler(res){
this.result = res;
}.bind(this))
.catch(function(error){
self.result = error;
});
}
})();
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta name="description" content="Example of promise with services in angularJs">
<script src="https://rawgit.com/lodash/lodash/3.0.1/lodash.min.js"><\/script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"><\/script>
<meta charset="utf-8">
<title>Matteo Playground</title>
</head>
<body ng-app="app">
<div ng-controller="MyCtrl as ctrl">
<h2>Current result: {{ctrl.result}}</h2>
</div>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">(function() {
'use strict';
angular.module('app', [])
.service('myRemoteService', MyRemoteService)
.controller('MyCtrl', MyCtrl);
function MyRemoteService($timeout, $q) {
this.defaultValue = 'default';
this.getUser = function getUser() {
return $timeout(function(){
return 'completato';
}, 3000)
.then(function(res){
return $timeout(function(){
return res + ' 2nd call';
}, 3000);
});
};
}
function MyCtrl(myRemoteService) {
var self = this;
this.result = myRemoteService.defaultValue;
myRemoteService.getUser()
.then(function resultHandler(res){
this.result = res;
}.bind(this))
.catch(function(error){
self.result = error;
});
}
})();
</script></body>
</html>
(function() {
'use strict';
angular.module('app', [])
.service('myRemoteService', MyRemoteService)
.controller('MyCtrl', MyCtrl);
function MyRemoteService($timeout, $q) {
this.defaultValue = 'default';
this.getUser = function getUser() {
return $timeout(function(){
return 'completato';
}, 3000)
.then(function(res){
return $timeout(function(){
return res + ' 2nd call';
}, 3000);
});
};
}
function MyCtrl(myRemoteService) {
var self = this;
this.result = myRemoteService.defaultValue;
myRemoteService.getUser()
.then(function resultHandler(res){
this.result = res;
}.bind(this))
.catch(function(error){
self.result = error;
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment