Answer the question
In order to leave comments, you need to log in
How can you pass $rootScope to an Angular 1.5 style component?
Once I followed tutorials even before 1.5 and after authentication wrote
$rootScope.authenticated = true;
$rootScope.currentUser = JSON.parse(localStorage.getItem('user');
and calmly used it wherever needed
Now I'm trying to refresh a little on 1.5 and of course I'm facing many changes.
I tried stupidly.
myApp
.component('home', {
bindings: {},
template: '<h1> Wellcome, {{$rootScope.currentUser}}{{currentUser}}</h1>',
controller: function ($rootScope) {
console.log($rootScope);
//я его нахожу где-то в дебрях, но в шаблон не лезет ни в какую
}
})
myApp.run(function($rootScope) {
var user = JSON.parse(localStorage.getItem('user'));
if(user) {
$rootScope.authenticated = true;
$rootScope.currentUser = user;
})}
myApp
.component('home', {
bindings: {},
template: '<h1> Wellcome, {{$ctrl.user}}</h1>',
controller: function (Auth) {
var $ctrl = this;
$ctrl.user = Auth.getAuthUser();
}
})
Answer the question
In order to leave comments, you need to log in
$rootScope is still perfectly injected, only you assign a value from it to some property in the controller.
function ($rootScope) {
var vm = this;
vm.user = $rootScope.currentUser;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question