我想创建一个执行登录功能的服务吗?

时间:2015-06-09 07:29:23

标签: angularjs validation model-view-controller service factories

这是我的app.js,我在其中创建了一个服务,该服务应该检查是否应该完成此特定用户名和密码填写登录,并且页面应该重定向到home.html 我无法获得如何将该特定服务调用到控制器以及我是否已正确创建服务。

我还需要检查两个字段是否都为空它是否为空提交按钮应该被禁用。

 app.js 

    // create angular app
    var validationApp = angular.module('validationApp',['ngRoute']);
    validationApp.config(
        function($routeProvider) {
            $routeProvider
                .when('/', {
                    templateUrl: 'main.html',
                    controller: 'mainController'
                })
                .when('/home', {
                    templateUrl: 'home.html',
                    controller: 'mainController'
                })
                .otherwise({
                    redirectTo: '/main.html'
                });
        });

    validationApp.service('loginservice',function()
    {
        this.login = function($scope,$location){
            var username = $scope.user.email;
            var password = $scope.user.password;
            if(username == "ank@gmail.com" && password=="1234")
            {
                if ($scope.userForm.$valid) {
                    alert('thank you for submitting your form');
                    $location.path("/home");

                }

            }
            else
            {
                alert("invalid username and password");
            }
        }
    });


    // create angular controller
    validationApp.controller('mainController', function($scope,loginservice) {

    $scope.dologin = loginservice.login();


    });

//这是我的index.html页面

<body>
<div  ng-app="validationApp">
   <ng-view></ng-view>

</body>

//这是我的main.html页面

main.html

<div class="container">
    <div class="row">
        <div>
            <form name="userForm" method="post" ng-submit="dologin()" novalidate>
                <h1>Login Form</h1>
                <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine }">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" ng-model="user.email" value="ank@gmail.com">
                    <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p>
                </div>

                <div class="form-group" ng-class="{ 'has-error' : userForm.password.$invalid && !userForm.password.$pristine }">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" ng-model="user.password" value="1234">
                    <p ng-show="userForm.password.$invalid && !userForm.password.$pristine" class="help-block">Enter a valid password.</p>
                </div>
                <label>Click me to toggle: <input type="checkbox" ng-model="checked"></label><br/>
                <button ng-model="button" ng-disabled="checked">Button</button>
                <button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid" >Submit</button>

            </form>
        </div>

    </div>
</div>

2 个答案:

答案 0 :(得分:0)

首先,您可以考虑使用路由的resolve参数禁止访问它,您必须在其中调用promise。如果promise是解析,则授予访问权限并触发$ routeChangeSuccess事件,否则您可以重定向到另一个页面,侦听$ routeChangeError事件。 see doc

为了做到这一点,您可以从您的登录服务返回承诺:

Price belongs_to Book
Book  has_many Price

并在您的路线中:

validationApp.service('loginService',function($q)
{
    var defer = $q.defer();
    this.login = function(username,password){

        if(username == "ank@gmail.com" && password=="1234")
        {
            defer.resolve({username:username});
        }
        else
        {
            defer.reject({error:"invalid username and password"});
        }
    }

    return defer.promise;
});

答案 1 :(得分:0)

看看是否有效,

DELETE FROM test5 x
WHERE x.ROWID >
ANY( select y.ROWID
FROM test5 y
WHERE X.AA = Y.AA
    AND
      X.BB = Y.BB
    AND
      X.CC = Y.CC)
AND (X.AA, X.BB, X.CC) IN (
SELECT AA, BB, CC FROM TEST5
GROUP BY AA, BB, CC
HAVING COUNT(AA) > 3);

在您的控制器中

validationApp.service('loginservice',function()
{
    this.login = function(username, password){
        if(username == "ank@gmail.com" && password=="1234")
        {
             alert('thank you for submitting your form');
             $location.path("/home");
        }
        else
        {
            alert("invalid username and password");
        }
    }
});