D
D
DKit2016-12-03 09:07:14
Angular
DKit, 2016-12-03 09:07:14

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);
//я его нахожу где-то в дебрях, но в шаблон не лезет ни в какую
  	}
})

Did it in..
myApp.run(function($rootScope) {
                var user = JSON.parse(localStorage.getItem('user'));
                if(user) {
                    $rootScope.authenticated = true;
                    $rootScope.currentUser = user;
})}

As a result, I did it through a factory.
myApp
  .component('home', {
    bindings: {},
  	template:  '<h1> Wellcome, {{$ctrl.user}}</h1>',
  	controller: function (Auth) {
      var $ctrl = this;
      $ctrl.user = Auth.getAuthUser();
  }
})

I understand that most likely the answer will be "no way, forget it", but still - how to use $rootScope?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2016-12-03
@healqq

$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;
}

In the template:
Please note that to do this is to put sticks in your wheels. Why are you not satisfied with the presence of the factory?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question