html:
<button class="submit" ng-disabled="dataloading" ng-class="{'vloader':newclass}" type="submit" flex="none"></button>
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;
authenticationService.login($scope.username, $scope.password, function(response){
if(response.data.status_code == 200){
$window.location.href = 'view/home.html';
}
});
}
};
}]);
service:
loginModule.factory('authenticationService', ['$http', '$cookieStore', '$rootScope', '$timeout', function($http, $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('error');
//need to set newclass and dataloading when http error occurs here ??
})
}
return service;
}])
I'm trying to do a login page with angular, the variables newclass and dataloading above are to change the class and to disable button after submitting form. I have some questions here
- I don't know how to change the value of newclass and dataloading if http error occurs (there is no way to access these two variables in the comment line in service).
- How should I encode json data (username and password) when sending them?