Search code examples
angularjstypescriptrootscope

$rootScope with TypeScript and controllerAs


I am building an application with Angular 1 and Typescript. Following is my Login Controller

module TheHub {
    /**
     * Login page controller.
     */
    export class LoginController {

        static $inject = ['$http', '$rootScope', '$location'];

        constructor(private $http: ng.IHttpService, private $rootScope, private $location: ng.ILocationService) {

        }

        /**
         * Method to check if the user is logged in 
         */
        login(user: {}) {
            this.$http.post('/login', user).then((value: ng.IHttpPromiseCallbackArg<{}>) => {
                this.$rootScope.auth = { isAuthenticated: true, isAuthenticationChecked: true };
                this.$location.url('/');
            }, (error: any) => {
                this.$rootScope.auth = { isAuthenticated: false, isAuthenticationChecked: true };
            });
        }
    }

    angular.module('TheHub').controller('LoginController', LoginController);
}

Now my app is structured in such a way that "App" is the main controller and "LoginController" is a controller nested under app. As you can see from the code, I am attempting to change something in $rootScope so that the view referring to App can get refreshed after login process is successful.

Somehow, there seems to be no effect at all. I checked it stackoverflow and found this question.

Define and Access $rootScope with controller as syntax

They are suggesting having a service written to abstract out the authentication information which I agree with but what about the two way data binding?

For example, I have a menu in App controller's view which I want to show when Login Controller finishes the login process successfully.

Update

App Controller:

module TheHub {
    /**
     * Root controller.
     */
    export class AppController {

        static $inject = ['$mdSidenav', '$rootScope'];

        constructor(private $mdSidenav: angular.material.ISidenavService, private $rootScope) {
        }

        /**
         * Handler for button click to show/hide left menu.
         */
        openLeftMenu = () => {
            this.$mdSidenav('left').toggle();
        }
    }

    angular.module('TheHub').controller('AppController', AppController);
}

View:

<div id="sideNavContainer" ng-controller="AppController as ctrl" layout="column" ng-cloak layout-fill>
    <md-toolbar flex="none">
        <div class="md-toolbar-tools">
            <md-button class="md-icon-button" aria-label="Settings" hide-gt-md ng-click="ctrl.openLeftMenu()">
                <i class="material-icons">menu</i>
            </md-button>
            The Hub
            <span flex></span>
        </div>
    </md-toolbar>
    <md-content flex layout="row">
        <md-sidenav ng-show="ctrl.auth.isAuthenticated" class="md-sidenav-left" md-component-id="left" md-is-locked-open="$mdMedia('gt-md')" md-disable-backdrop md-whiteframe="4" flex="none">
            <md-content layout-padding>

            </md-content>
        </md-sidenav>
        <div ng-view flex="grow"></div>

    </md-content>
</div>

Solution

  • Your view looks for the auth object in ctrl.auth. So it expects it to be an attribute of AppController. But it's not. It's an attribute of $rootScope. Since every expression is evaluated on the scope, and every scope inherits from $rootScope, all you need is

    ng-show="auth.isAuthenticated"