vahid najafi vahid najafi - 3 months ago 11
Javascript Question

Angularjs read $scope in only root state

I'm using

ionic
and
angularjs
.

My app.

.state('auth', {
url: '/auth',
abstract:true,
templateUrl: 'templates/auth.html',
controller: 'authCtrl'
})
.state('auth.reg', {
url: '/reg',
templateUrl: 'templates/register.html',
controller: 'regCtrl'
});


I have this in my
auth.html
(root view) :

{{authHeader}}


My
controller.js


.controller('authCtrl', function($scope, $stateParams) {
// here can get the scope
// $scope.authHeader = "register";
})


.controller('regCtrl', function($scope, $stateParams) {
// here CAN NOT get the scope
$scope.authHeader = "register";
});


Only in root controller I can get the
$scope
. I want to add another controller called
login
, and change there to login. Any idea?

Answer

You can use $rootScope for this purpose

.controller('authCtrl', function($scope, $rootScope, $stateParams) {
  // here can get the scope
  // $rootScope.authHeader = "register";
})

And in reg:

.controller('regCtrl', function($scope, $rootScope, $stateParams) {
  // here CAN NOT get the scope
  $rootScope.authHeader = "register";
});
Comments