My service is:
JavaScript
x
20
20
1
myApp.service('userService', [
2
'$http', '$q', '$rootScope', '$location', function($http, $q, $rootScope, $location) {
3
var deferred;
4
deferred = $q.defer();
5
this.initialized = deferred.promise;
6
this.user = {
7
access: false
8
};
9
this.isAuthenticated = function() {
10
this.user = {
11
first_name: 'First',
12
last_name: 'Last',
13
email: 'email@address.com',
14
access: 'institution'
15
};
16
return deferred.resolve();
17
};
18
}
19
]);
20
I’m calling this in my config
file via:
JavaScript
1
12
12
1
myApp.run([
2
'$rootScope', 'userService', function($rootScope, userService) {
3
return userService.isAuthenticated().then(function(response) {
4
if (response.data.user) {
5
return $rootScope.$broadcast('login', response.data);
6
} else {
7
return userService.logout();
8
}
9
});
10
}
11
]);
12
However, it complains that then
is not a function. Aren’t I returning the resolved promise?
Advertisement
Answer
From your service method:
JavaScript
1
8
1
function serviceMethod() {
2
return $timeout(function() {
3
return {
4
property: 'value'
5
};
6
}, 1000);
7
}
8
And in your controller:
JavaScript
1
7
1
serviceName
2
.serviceMethod()
3
.then(function(data){
4
//handle the success condition here
5
var x = data.property
6
});
7