0

I have my body controlled by a MainController. In the body, I have another nested controller. In my index.html, I have a div element controlled by the nested controller:

index.html

<body ng-controller="MainController">
  <div ng-controller="LoginController" ng-hide="isLoggedIn"></div>
  <div class="navbar" ng-hide="!isLoggedIn">
    <!-- A form which calls login() function inside LoginController -->
  </div>
</body>

MainController:

angular.module('DemoApp')
    .controller('MainController', ['$scope', function ($scope) {
    $scope.isLoggedIn = false;
}]);

LoginController

angular.module('DemoApp')
    .controller('LoginController', ['$scope', '$location', function ($scope, $location) {
    $scope.login = function () {
       if($scope.loginCredentials.username === 'username' && $scope.loginCredentials.password === 'password') {
            $scope.parent.isLoggedIn = true; /* here, how to modify this variable */
            $location.path('/home');
        }
   };
}]);

All I want to do is change the variables of MainController, which is isLoggedIn, from my nested controllers. I used $scope.parent, but it shows unknow provider parent. How to achieve this?

2
  • 1
    Use $localStorage to store isLoggedIn. and get it from $localStorage when needed. This data is available until you manually clear the key. Commented Jan 5, 2017 at 6:40
  • @SharanDeSilva thanks, I'll use that in the future. Commented Jan 5, 2017 at 6:44

1 Answer 1

2

You need to use $parent to get the parent controller scope, then various methods and properties can be accessed.

$scope.$parent.isLoggedIn = true;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Also, if I go on home page again, I need to set that variable to false. How to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.