Angularjs angular指令验证服务中的所有逻辑

Angularjs angular指令验证服务中的所有逻辑,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我应该管理一个登录指令 表单,因为添加了一个msg以防万一 凭据错误,因此我最终得到: HTML 请登录 记得我吗 JS .factory('Auth',函数($http,$q,$window){ 返回{ 发送:功能(数据){ var deferred=$q.deferred(); $http.post('/user/auth',data) .成功(功能(响应){ 延迟。解决(响应); }) .error(函数(数据、状态、标题、配置){ 延期。拒绝([]); }); 回报。承诺; }, 验

我应该管理一个登录指令 表单,因为添加了一个msg以防万一 凭据错误,因此我最终得到:

HTML

请登录
记得我吗
JS
.factory('Auth',函数($http,$q,$window){
返回{
发送:功能(数据){
var deferred=$q.deferred();
$http.post('/user/auth',data)
.成功(功能(响应){
延迟。解决(响应);
})
.error(函数(数据、状态、标题、配置){
延期。拒绝([]);
});
回报。承诺;
},
验证:函数(数据){
}
}
})
.controller('MainCtrl',函数($scope,Auth){
$scope.user={};
})
.directive('auth',function($window,auth){
返回{
限制:“A”,
控制器:函数($scope$element){
$scope.getAuth=function(){
Auth.send($scope.user)。然后(
功能(响应){
if((type of response.data!=='undefined')&&(response.data===$scope.user.email)){
$window.location.href='1http://localhost:3000/admin';
}
},
功能(拒绝){
$(“#无效登录”).remove();
$element.prepend(“

无效登录”

”; } ); }; } }; });
我想把所有的逻辑都放在服务中 但是我找不到路^^

编辑
.factory('Auth',函数($http,$q,$window){
返回{
发送:功能(数据){
var deferred=$q.deferred();
$http.post('/user/auth',data)
.成功(功能(响应){
延迟。解决(响应);
})
.error(函数(数据、状态、标题、配置){
延期。拒绝([]);
});
回报。承诺;
},
身份验证:函数(数据、callBackOnError){
这个。发送(数据)。然后(
功能(响应){
if((type of response.data!=='undefined')&&(response.data==data.email)){
$window.location.href='1http://localhost:3000/admin';
}
},
回拨器
);
}
}
})
.controller('MainCtrl',函数($scope,Auth){
$scope.user={};
})
.directive('auth',function($window,auth){
返回{
限制:“A”,
控制器:函数($scope$element){
$scope.getAuth=function(){
认证(
$scope.user,
功能(拒绝){
$(“#无效登录”).remove();
$element.prepend(“

无效登录”

”; } ); }; } }; });

不错^^

更改HTML,以便对getAuth的调用将用户作为参数

<form role="form" name="form" data-auth="" data-ng-submit="getAuth(user)" class="form-signin">  
    <h2 class="form-signin-heading">Please sign in</h2>
    <input type="text" name="email" placeholder="Email address" required="required" autofocus="autofocus" data-ng-model="user.email" class="form-control">
    <input type="password" name="password" placeholder="Password" required="required" data-ng-model="user.password" class="form-control">
    <label class="checkbox">
      <input type="checkbox" value="remember-me">Remember me
    </label>
    <input type="submit" value="Sign in" data-ng-disabled="form.$invalid" class="btn btn-lg btn-primary btn-block">

您可以创建一个用于处理ajax请求的服务,另一个用于处理业务逻辑(如果您正在寻找的话)。感谢您的建议,但是服务中缺少$element,总之,我讨厌在服务中管理html
.factory('Auth', function($http,$q,$window) {
        return {
            send : function(data){
                var deferred = $q.defer();
                $http.post('/user/auth',data)
                    .success(function (response) {
                        deferred.resolve(response);
                    })
                    .error(function(data, status, headers, config) {
                        deferred.reject([]);
                    });
                return deferred.promise; 
            },
            authenticate:function(data){

            }
        }
    })
    .controller('MainCtrl', function ($scope,Auth) {
            $scope.user = {};
     })
     .directive('auth',function($window,Auth) {
            return {
                restrict: 'A',
                controller: function($scope,$element) {
                    $scope.getAuth = function(){
                        Auth.send($scope.user).then(
                            function(response){
                                if( (typeof response.data !== 'undefined') && (response.data === $scope.user.email)){
                                   $window.location.href = 'http://localhost:3000/admin';
                                }
                            },
                            function(reject){
                                $('#invalid-login').remove();
                                $element.prepend('<p id="invalid-login" class="text-danger text-center">Invalid login</p>');
                            }
                        ); 

                    };
                }
            };
        });
.factory('Auth', function($http,$q,$window) {
        return {
            send : function(data){
                var deferred = $q.defer();
                $http.post('/user/auth',data)
                    .success(function (response) {
                        deferred.resolve(response);
                    })
                    .error(function(data, status, headers, config) {
                        deferred.reject([]);
                    });
                return deferred.promise; 
            },
            authenticate:function(data,callBackOnError){
                this.send(data).then(
                    function(response){
                        if( (typeof response.data !== 'undefined') && (response.data === data.email)){
                            $window.location.href = 'http://localhost:3000/admin';
                        }
                    },
                    callBackOnError
                );
           }
        }
    })
    .controller('MainCtrl', function ($scope,Auth) {
        $scope.user = {};
     })
    .directive('auth',function($window,Auth) {
        return {
            restrict: 'A',
            controller: function($scope,$element) {
                $scope.getAuth = function(){
                    Auth.authenticate(
                        $scope.user,
                        function(reject){
                            $('#invalid-login').remove();
                            $element.prepend('<p id="invalid-login" class="text-danger text-center">Invalid login</p>');
                        }
                    ); 
                };
            }
        };
    });
<form role="form" name="form" data-auth="" data-ng-submit="getAuth(user)" class="form-signin">  
    <h2 class="form-signin-heading">Please sign in</h2>
    <input type="text" name="email" placeholder="Email address" required="required" autofocus="autofocus" data-ng-model="user.email" class="form-control">
    <input type="password" name="password" placeholder="Password" required="required" data-ng-model="user.password" class="form-control">
    <label class="checkbox">
      <input type="checkbox" value="remember-me">Remember me
    </label>
    <input type="submit" value="Sign in" data-ng-disabled="form.$invalid" class="btn btn-lg btn-primary btn-block">
.factory('Auth', function($http,$q,$window) {
    return {
        send : function(data){
            var deferred = $q.defer();
            $http.post('/user/auth',data)
                .success(function (response) {
                    deferred.resolve(response);
                })
                .error(function(data, status, headers, config) {
                    deferred.reject([]);
                });
            return deferred.promise; 
        },
        authenticate:function(data){

        },
        getAuth: function(user){
            send(user).then(
                function(response){
                    if( (typeof response.data !== 'undefined') && (response.data === $scope.user.email)){
                        $window.location.href = 'http://localhost:3000/admin';
                }
            },
            function(reject){
                $('#invalid-login').remove();
                $element.prepend('<p id="invalid-login" class="text-danger text-center">Invalid login</p>');
            }
        ); 

    }
}
})
 .controller('MainCtrl', function ($scope,Auth) {
        $scope.user = {};
        $scope.getAuth = Auth.getAuth;
 })