如果输入在离子状态为空时如何不发送请求

时间:2017-06-16 09:25:30

标签: javascript json http ionic-framework

我有一个$ http请求。当我按下按钮时,请求被触发。但是我想阻止请求,如果表单的输入是空的。在我尝试使用必需的输入。但它不起作用< / p>

$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>

在按钮中有提交功能。所以它仍会发送请求。我希望只有当用户输入他的登录名和密码才发送请求,或者如果输入为空则禁用按钮

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

//检查null并调用$ http

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

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