Reputation: 10009
I'm trying to build a Session service in AngularJS, but I'm having trouble storing values in it. Am I storing or fetching the values wrong? The pattern I'm trying to implement is described by Gert Hengeveld in his article Techniques for authentication in AngularJS applications
app/assets/javascripts/app/controllers/SessionCtrl.js.coffee
app = angular.module "my_app"
app.controller 'SessionCtrl', ['$scope', 'Session', '$http', '$window',
($scope, Session, $http, $window) ->
getUserToken = (username, password) ->
params = { grant_type: "password", username: username }
$http(
method: "POST"
url: "http://app.dev/oauth/token"
params: params
).success((data, status, headers, config) ->
console.log('getUserToken ' + data.access_token) // Outputted with the token I received.
Session.create(data.access_token)
).error (data, status, headers, config) ->
$scope.hi_there = 'martin'
services/session.js
app = angular.module('trenger')
app.service('Session', function () {
this.create = function (accessToken) {
console.log('in Sesssion create: ' + accessToken); // Seen in the console
this.accessToken = accessToken;
};
this.destroy = function () {
this.accessToken = null;
};
console.log('accessToken seen from the service: ' + this.accessToken ); // Not seen in console
return this;
});
application.html
<strong> Session.accessToken {{ Session.accessToken }} </strong> # Nothing
<em> Other val from SessionCtrl: {{ hi_there }} </em> # This works
Upvotes: 0
Views: 111
Reputation: 23652
userId
is a property, not a function, therefore
console.log(Session.userId)
Would work better.
Upvotes: 4