Angularjs使用Cookie登录应用程序

时间:2016-07-15 14:08:46

标签: angularjs login angular-cookies

我尝试使用Angularjs创建一个登录应用程序,该应用程序将信息存储在$ cookies中。用户登录后,无需再次登录:

这是登录表单:

<form action="/" id="login">
    <div class="form-group">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" class="form-control" ng-model="username" required />

    </div>
    <div class="form-group">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" class="form-control" ng-model="password" required />
    </div>
    <div class="form-actions">
        <button id="submit_btn" type="button" ng-click="submit()" class="btn btn-danger">Login</button>

    </div>
</form> 

这是app.js

var app = angular.module('mailApp', ['angular.chosen','ngRoute','ngCookies'], function(){});

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

    $routeProvider.
        when('/', {
            templateUrl: 'login.html',
        }).
        when('/logs/', {
            templateUrl: 'index.html',
            controller: 'mailController',
        }).
        otherwise({
            redirectTo: '/'
        });

    }]);

app.controller('mailController', function($scope, $http,$routeParams,$location,domainService, $rootScope, $location, $cookies)
{
    $rootScope = false;
    $scope.submit = function (info) {

        if($scope.username == '123' && $scope.password == '123') {
            $rootScope.loggedIn = true;
            $location.path('/logs/');
        }
        else{
           alert('Wrong authentication!');
            }
        }
//... 
}

但我是angularjs的新手并没有真正得到$ cookies。

任何帮助将不胜感激!

谢谢,伙计们!

0 个答案:

没有答案
相关问题