简体   繁体   中英

how to not send request if inputs are empty in ionic

i have an $http request.when i press the button,the request is fired.but i want to block the request if the form's inputs are empty.in the inputs i tried with Required.but it didn't work

 $scope.submit=function() { var loginServiceUrl = 'https://www.yabiladi.com/newsapi/login.json'; var loginServiceData = { user: $scope.username, password: $scope.password } $http.post(loginServiceUrl, loginServiceData). then(function (res){ $scope.drapeau=0; console.log(res); $scope.user=res.config.data.user; }).catch(function(response){ //rien }) } 
  <div ng-if="flag==2" ng-controller="loginCtrl"> <div ng-show="drapeau"> <label for="field1"><span>Username:</span> <div class="loginform"><input type="text" class="input-field" name="field1" value="" ng-model="username" /></div></label> <label for="field2"><span>Password:</span><div class="loginform"><input type="password" class="input-field" name="field2" value="" ng-model="password" /></div></label> <div class="boutonlogin"> <button class="button button-energized button-small" ng-click="submit()" style="width:80px">Login</button> </div> <label> <b><center>if you don't have an account,you can signup</center></b> </label> <div class="boutonlogin"> <button class="button button-energized button-small" ng-href="#/signup/{{flag}}" style="width:80px" ng-click="modal.hide()">Signup</button> </div> </div> <div ng-hide="drapeau"> user connected : {{user}} </div> </div> 

in the button there's that submit function.so it will send the request anyway.i want the request to be sent only if the user enters his login and password or disable the button if the inputs are empty

thank you for the help

//check null and call $http

if(loginServiceData.user!=null && loginServiceData.password!=null){
// here you can use your $http call
//$http.post()

}else{
     alert("Please Enter Username && Password");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM