【发布时间】:2018-10-03 15:03:49
【问题描述】:
登录.js:
app.controller('LoginFormController', ['$scope','$http','$rootScope', '$state', function($scope, $http, $rootScope, $state) {
$scope.user = {};
$scope.authError = null;
$scope.login = function() {
$scope.authError = null;
var emailId = $scope.user.email;
var password = $scope.user.password;
$http({
url: 'http://localhost:8090/login/login',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: 'email='+emailId+'&password='+password
//data: {'email': $scope.user.email, 'password': $scope.user.password}
}).then(function(response) {
console.log(response.data);
if (response.data.status == 'SUCCESS') {
$scope.user = response.data.user.firstName;
$rootScope.test = response.data.user.firstName;
console.log("check: ",$rootScope.test)
$state.go('app.dashboard');
} else {
//alert('invalid login');
$scope.authError = 'Email or Password not right';
}
}, function(x) {
$scope.authError = 'Server Error';
})
};
}])
我将值保存在 $rootScope.test 下
这是我的 App.main.js:
'use strict';
angular.module('app').controller('AppCtrl', ['$scope','$rootScope',
function($scope, $rootScope) {
$scope.user5 = $rootScope.test;
}
]);
试图打印根范围
如果我运行此代码,我将面临 $rootScope 在控制台中未定义的错误。如何解决这个问题
【问题讨论】:
-
哪个控制器出错了
-
你试过在 main.js 中使用
$scope.test吗? -
不要将数据存储在 $rootScope 上,而是使用服务。 $rootScope 已被弃用,使用它会使迁移到 Angular 2+ 变得更加困难。另见AngularJS FAQ - $rootScope exists, but it can be used for evil
标签: angularjs angular-ui-router rootscope