Angularjs $rootScope,带TypeScript和controllerAs
我正在用Angular 1和Typescript构建一个应用程序。下面是我的登录控制器Angularjs $rootScope,带TypeScript和controllerAs,angularjs,typescript,rootscope,Angularjs,Typescript,Rootscope,我正在用Angular 1和Typescript构建一个应用程序。下面是我的登录控制器 module TheHub { /** * Login page controller. */ export class LoginController { static $inject = ['$http', '$rootScope', '$location']; constructor(private $http: ng.IHttpS
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);
}
视图:
<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>
菜单
枢纽
视图在ctrl.auth
中查找auth
对象。因此它希望它是AppController
的一个属性。但事实并非如此。它是$rootScope
的一个属性。因为每个表达式都在作用域上求值,并且每个作用域都继承自$rootScope,所以您只需要
ng-show="auth.isAuthenticated"
如果看不到应用程序控制器及其视图,就很难诊断问题。
ng-show="auth.isAuthenticated"