javascript - Using $watch on an AngularJS service function defined as an ES6 class -
i'm using es6 angularjs , following this guide. recommends defining services classes.
in service definition i'm injecting $cookiestore service , attaching class this:
export default class authentication { constructor($cookiestore) { this.$cookiestore = $cookiestore; } setauthenticatedaccount(account) { this.$cookiestore.put(json.stringify(account), 'authenticatedaccount'); } isauthenticated() { return !!this.$cookiestore.get('authenticatedaccount'); } }
i want update navbar reflect whether or not user logged in, i'm doing $watch
:
var navbarcontroller = function($location, $scope, authentication) { $scope.$watch(authentication.isauthenticated, () => { $scope.isauthenticated = authentication.isauthenticated(); $scope.currentuser = authentication.getauthenticatedaccount(); }); }
this gives following error:
cannot read property '$cookiestore' of undefined
.
this error occurs when don't inject proper dependencies in controller.
just make sure $cookiestore
propertly injected in angular controller
myapp.controller('myctrl', ['$scope', '$cookiestore', function ($scope, $cookiestore) { ... ...
point note
$cookiestore service deprecated. please use $cookies service instead.
Comments
Post a Comment