I want to create AngularJS service to return data from another service.
I tried to use this code:
'use strict';
angular.module('angularDjangoRegistrationAuthApp')
.service('currentUser', function (djangoAuth) {
var that = this;
djangoAuth.profile().then(function(data) {
that.username = data.username;
});
this.getUsername = function() {
return this.username;
};
});
But when I tried to get username from controller like this:
$scope.username = currentUser.getUsername();
It was empty.
In currentUser service you are setting the username by
but in this.getUsername method you are returning it with this.username which is not defined. so try changing it to
if djangoAuth service is getting data with profile method correctly then this code should work
OR you can change following code too: