Skip to content

Instantly share code, notes, and snippets.

View gsans's full-sized avatar
🚁
Hawaii is Awesome

Gerard Sans gsans

🚁
Hawaii is Awesome
View GitHub Profile
var deferred = $q.defer();
var promise = deferred.promise;
//basic version
promise.then(fnSuccess)
.catch(fnFailure) //optional
.finally(fnAlways) //optional
//advanced version
promise.then(fnSuccess, fnFailure, fnNotification)
.catch(fnFailure) //optional
deferred.resolve(resultObj); //triggers fnSuccess
deferred.reject(reasonObj); //triggers fnFailure
deferred.notify(progressObj); //triggers fnNotification
$http.get('movies.json')
.then(function(response){
$scope.movies= response.data;
})
.catch(function(error){
console.log(error);
});
app.factory("Movies", function($http, $q) {
return {
get: function() {
var deferred = $q.defer();
$http.get('movies.json')
.then(function(response){
deferred.resolve(response.data);
})
.catch(function(error){
deferred.reject(error);
.run(function($httpBackend, Users) {
//mock users api
$httpBackend.whenGET('/api/users')
.respond( Users.get() );
//you can mock GET, POST, HEAD, PUT, DELETE, PATCH, JSONP. See $httpBackend help.
// allow other requests to use $http. Eg: templates
$httpBackend.whenGET(/.tpl.html$/).passThrough();
})
$http({method: 'GET', url: '/api/Users'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
$scope.users = data;
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
angular.module(“myapp”, [‘ngMockE2E’])
<profile onAlert=”alert(message)”></profile>
scope : { local_onAlert: “&onAlert” } //using alias
scope : { onAlert: “&” } //abbreviation when using identical names
//Usage:
// local_onAlert({message:”hey”});
// onAlert({message:”hey”});
<profile onAlert=”alert(message)”></profile>
scope : { local_onAlert: “&onAlert” } //using alias
scope : { onAlert: “&” } //abbreviation when using identical names
//Usage:
// local_onAlert({message:”hey”});
// onAlert({message:”hey”});