修改 $rootscope 时变量 class 未更改
Variable class not changed when modify $rootscope
我有两个控制器:
父控制器
'use strict';
app.controller('CoreController', function($scope,$window,$location,$rootScope,AuthFactory) {
var requiredLogin = true;
$rootScope.bodyClass = '';
$rootScope.pageTitle = '';
$scope.Usuario = AuthFactory.user();
});
子控制器
'use strict';
app.controller('SignController', function($scope, $controller, $stateParams,$rootScope, AuthFactory) {
angular.extend(this, $controller('CoreController', {$scope: $scope}));
$scope.EmailDefault = $stateParams.email;
if(angular.isDefined($stateParams.pass)){
$scope.SpecifyPass = $stateParams.pass;
}else{
$scope.SpecifyPass = true;
}
switch($stateParams.mode) {
case "github":
$rootScope.bodyClass = "dgdfg";
break;
case "google":
$rootScope.bodyClass = "login_registro";
break;
default:
$rootScope.bodyClass = "login_registro";
}
});
我这样设置class:
<body id="inicio" ng-class="bodyClass" data-spy="scroll" data-target=".navbar-fixed-top" data-offset="200">
但是当我尝试修改 $rootscope 时 class 没有改变。
我做错了什么?
试试这个,
<body id="inicio" ng-class="$root.bodyClass" data-spy="scroll" data-target=".navbar-fixed-top" data-offset="200">
最好完全不使用 rootscope。
我解决了在子控制器中将 "statechangedsucces" 事件添加到我的 switch 语句中的问题:
'use strict';
app.controller('SignController', function($scope, $controller, $stateParams,$rootScope, AuthFactory) {
angular.extend(this, $controller('CoreController', {$scope: $scope}));
$scope.EmailDefault = $stateParams.email;
if(angular.isDefined($stateParams.pass)){
$scope.SpecifyPass = $stateParams.pass;
}else{
$scope.SpecifyPass = true;
}
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
switch($stateParams.mode) {
case "github":
$rootScope.bodyClass = "login_github";
break;
case "google":
$rootScope.bodyClass = "login_google";
break;
default:
$rootScope.bodyClass = "login_registro";
}
});
});
我有两个控制器:
父控制器
'use strict';
app.controller('CoreController', function($scope,$window,$location,$rootScope,AuthFactory) {
var requiredLogin = true;
$rootScope.bodyClass = '';
$rootScope.pageTitle = '';
$scope.Usuario = AuthFactory.user();
});
子控制器
'use strict';
app.controller('SignController', function($scope, $controller, $stateParams,$rootScope, AuthFactory) {
angular.extend(this, $controller('CoreController', {$scope: $scope}));
$scope.EmailDefault = $stateParams.email;
if(angular.isDefined($stateParams.pass)){
$scope.SpecifyPass = $stateParams.pass;
}else{
$scope.SpecifyPass = true;
}
switch($stateParams.mode) {
case "github":
$rootScope.bodyClass = "dgdfg";
break;
case "google":
$rootScope.bodyClass = "login_registro";
break;
default:
$rootScope.bodyClass = "login_registro";
}
});
我这样设置class:
<body id="inicio" ng-class="bodyClass" data-spy="scroll" data-target=".navbar-fixed-top" data-offset="200">
但是当我尝试修改 $rootscope 时 class 没有改变。
我做错了什么?
试试这个,
<body id="inicio" ng-class="$root.bodyClass" data-spy="scroll" data-target=".navbar-fixed-top" data-offset="200">
最好完全不使用 rootscope。
我解决了在子控制器中将 "statechangedsucces" 事件添加到我的 switch 语句中的问题:
'use strict';
app.controller('SignController', function($scope, $controller, $stateParams,$rootScope, AuthFactory) {
angular.extend(this, $controller('CoreController', {$scope: $scope}));
$scope.EmailDefault = $stateParams.email;
if(angular.isDefined($stateParams.pass)){
$scope.SpecifyPass = $stateParams.pass;
}else{
$scope.SpecifyPass = true;
}
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
switch($stateParams.mode) {
case "github":
$rootScope.bodyClass = "login_github";
break;
case "google":
$rootScope.bodyClass = "login_google";
break;
default:
$rootScope.bodyClass = "login_registro";
}
});
});