Last active
August 29, 2015 14:15
-
-
Save cef62/ebc2f5ecccd2b8932438 to your computer and use it in GitHub Desktop.
Example of promise with services in angularJs // source https://jsbin.com/reluge/2
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> | |
<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> |
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
(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