javascript - angularjs的问题
PHPz
PHPz 2017-04-10 16:51:38
0
3
169

controller:

loginModule.controller("loginController", ['$window', '$rootScope', '$scope', '$location', 'authenticationService', function($window, $rootScope, $scope, $location, authenticationService){
    $scope.newclass = false; 
    $scope.login = function(){
        if($scope.form_login.$invalid){
            return false;
        }else{
            $scope.newclass = true;       //按钮样式
            $scope.dataloading = true;    //ng-disabled禁用按钮
            authenticationService.login($scope.username, $scope.password, function(response){
                if(response.data.status_code == 200){
                    $window.location.href = 'view/home.html';
                }else if{
                    //其他错误状态
                }
            });
        }
    };
}]);

service:

loginModule.factory('authenticationService', ['$http', '$base64', '$cookieStore', '$rootScope', '$timeout', function($http, $base64, $cookieStore, $rootScope, $timeout){

    var service = {};

    service.login = function(username, password, callback){

        $http({
            method: 'POST',
            url: 'services/login',
            data: {usrname: username, password: password}
        }).then(function(response){
            callback(response);
        }, 
        function(response){
            alert('connection error');
            
            //这里想要改变controller里面newcalss和dataloading的值???
            
        })
    }
    return service;
}])

初学,在写一段用户登陆的功能,controller和service如上,我在controller里面定义了dataloading和newclass两个值来控制提交按钮的样式和禁用。
请问在service里HTTP请求错误后(注释处)想改变newclass和dataloading的值来控制按钮禁用应该怎么做?

PHPz
PHPz

学习是最好的投资!

reply all(3)
Peter_Zhu

service是给controller调用的,应该在controller里做改变状态处理

大家讲道理

factory创建的服务应该只关注请求,把你的authenticationService注入到loginController中,在loginController里调用service.login传入回调函数,再在回调函数里改变$scope下的值

洪涛

在你的controller中根据callback返回的data轻松就改变了啊

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template