我使用angular js为登录/注销功能实现了''cookieStore'''[ng-cookies]'。 我按照这里提到的文档(在Take2中):http://maffrigby.com/maintaining-session-info-in-angularjs-when-you-refresh-the-page/
这是我的代码段:
var app = angular.module('TaskManager', ['ngCookies']);
app.controller('LoginController', function($scope, $http, $location, $cookieStore) {
$scope.login = function(str) {
console.log(".......... login called......");
var validEmail = validateEmail(email.value);
if (validEmail && password.value != "") {
$http({
method: 'POST',
url: '../TaskManager/public/user/login',
data: $.param({
email: email.value,
password: password.value
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.success(function(data, status) {
console.log(data);
var result = data.response;
console.log(result);
if (result == "success") {
$scope.userId = data.user_id;
$scope.email = data.email;
$scope.Name = data.name;
$scope.password = data.password;
$scope.Type = data.type;
//storing value at cookieStore
$cookieStore.put("loggedin", "true");
document.getElementById('hiddenForm').setAttribute("action", "./Dashboard.html");
document.getElementById('hiddenForm').submit();
} else alert(data.message);
});
app.controller('DashboardController', function($scope, $http, $location, $cookieStore) {
$scope.loggedIn = $cookieStore.get("loggedin");
....................................
....................................
});
app.controller('ReportController', function($scope, $http, $location, $cookieStore) {
$scope.loggedIn = $cookieStore.get("loggedin");
....................................
....................................
});
app.controller('TaskController', function($scope, $http, $location, $cookieStore) {
$scope.loggedIn = $cookieStore.get("loggedin");
....................................
....................................
});
它适用于LoginController和DashboardController,但对于其余控制器,它会出现错误:未捕获错误:无模块:ngCookies 。
虽然我在所有页面中都有相同的代码。在html文件中,我将我的应用指定为ng-app =“TaskManager”:
<html ng-app="TaskManager">
错误:未捕获错误:没有模块:ngCookies ,对我来说是不可理解的。
任何建议都会受到赞赏。
答案 0 :(得分:0)
好像您可能忘记在不起作用的页面中包含ngCookies脚本。 :)