angular.module('site.controllers', ['ngCookies'])
.controller("LoginController", ["$scope", "$http", "$cookies", function($scope, $http, $cookies){
debugger; // can access $cookies here
$scope.login = function(){
debugger; // ReferenceError: $cookies is not defined
var credentials = {
'username': $scope.username,
'password': $scope.password
};
$http.post('/login', credentials)
.success( function(data, status, headers, config){
$scope.template = $scope.templates[2];
})
.error( function(data, status, headers, config){
debugger;
$scope.template = $scope.templates[2];
//TODO: info for user that login failed
});
};
}])
;
我不知道为什么 $cookies 服务在 LoginController 直接正文中可用,但在 LoginController 的登录功能中不可用。
$scope 和 $http 在这两个地方都可用,但是 $cookies 服务是有问题的。
这些文件包含在 HTML 头中:
<script type="text/javascript" src="static/lib/angular/angular.js"></script>
<script type="text/javascript" src="static/lib/angular/angular-cookies.js"></script>