Dear all,
I am using angularjs 1.4 with es6 classes, I also use "controller as"
syntax and I don't use $scope.
But i still need to use the $rootScope for common functions and
authentication context so they are accessible for every controller and for
every view.
export class Main {
constructor($location, $rootScope){
this.$location = $location;
$rootScope.copy = angular.copy;
$rootScope.go = (path) => this.$location.path(path); // especially
useful for buttons ng-click
// $rootScope.track = ... call to google analytics
}
};
Main is the controller of the main page, the one that contains the ng-view
In the login controller :
export class LoginController {
constructor($scope, $location, $http, $cookieStore, $rootScope,
$$webSocketService){
this.$location = $location;
this.$http = $http;
this.$cookieStore = $cookieStore;
this.$rootScope = $rootScope;
var credentials = this.$cookieStore.get("credentials");
if (credentials && credentials.mailAddress && credentials.password) {
this.logIn(credentials.mailAddress, credentials.password, null, ()
=> {
this.$cookieStore.remove("credentials")
});
}
}
logIn(mailAddress, password, successCallback, errorCallback) /*can be
called by the login form or by the constructor hereabove*/{
*// authenticate and set if value to *this.$rootScope.authzContext
}
logOut (){
this.$rootScope.authzContext = null;
this.$cookieStore.remove("credentials");
this.$location.path("/")
};
};
So any where if need to check the authzContext I can write
$rootScope.$watch('authzContext, ...
Example :
export class MovieController {
constructor($http, $routeParams, $rootScope){
this.$http=$http;
this.$routeParams = $routeParams;
this.obj = {}
$rootScope.$watch('authzContext', (authzContext) => {
if (authzContext) {
this.$http.get(GLOBALS.getUri("movies/{0}",
this.$routeParams.id)).success(x => this.obj = x)
}
})
}
}
Is it the right way to do ?
Many thanks,
- A
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.