2017-06-16 69 views
0

我有一個$ http請求。當我按下按鈕時,請求被解僱。但是我想阻止請求,如果表單的輸入爲空。在輸入i與Required.but試了一下沒在按鈕的工作如何在離子中輸入空時不發送請求

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

有這麼提交function.so它將發送請求anyway.i想請求,如果用戶只發送輸入他的登錄名和密碼或禁用按鈕,如果輸入爲空

感謝你的幫助

+0

如果(loginServiceData.user!= NULL && loginServiceData.user!=未定義&& loginServiceData.password!= NULL && loginServiceData。密碼!= undefined){// $ http } else {alert(「請輸入用戶名和密碼」);} –

+0

@KalaiselvanA似乎很簡單,雖然我沒有考慮它。 –

回答

0

//檢查空和調用$ HTTP

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

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