jsindos jsindos - 3 months ago 19
AngularJS Question

Setting an Angular service on an ES6 class property affects its functionality

I'm using ES6 with Angular following this guide http://angular-tips.com/blog/2015/06/using-angular-1-dot-x-with-es6-and-webpack/. It recommends defining services as classes. In a service definition I'm injecting the

$cookieStore
service and attaching it to the class:

export default class Authentication {
constructor($cookieStore) {
this.$cookieStore = $cookieStore;
}

setAuthenticatedAccount(account) {
this.$cookieStore.put(JSON.stringify(account), 'authenticatedAccount');
}
}


When
setAuthenticatedAccount
is called,
this.$cookieStore
executes but doesn't create the cookie
authenticatedAccount
. The cookie is created if you call
$cookieStore
without attaching it to
this
.

How can I define services as classes whilst avoiding this problem?

Answer

You call the put function the wrong way. Try with this parameter order:

this.$cookieStore.put('authenticatedAccount', JSON.stringify(account));
Comments