2016-09-26 26 views
1

我有一個輸入框按鈕的輸入值獲取有關點擊頁面

<div class="inputs"> 
     <label>Online Payment ID</label> 
     <input type="text" class="form-control" ng-model="payId"> 
</div> 

我有一個按鈕,頁面有些地方說

<button type="button" class="btn" ng-click="proceedPayment()" ></button> 
現在

上點擊proceedPayment()將被稱爲,從那裏我需要檢查上面的文本fild輸入的值使用

我試過,

$scope.proceedPayment = function() { 
     alert($scope.payId); // Always alerts undefined 
    if (!$scope.payId) { 
       $scope.payIdError = true; 
       console.log('No value entered'); 
      } else { 
       $scope.payIdError = false; 
       console.log('value entered'); 
      } 
} 

但這不起作用 我不想在這裏使用窗體。我是一個有角度的初學者,感謝您的幫助!

+0

@Satpal錯字錯誤! – monda

+0

在函數中訪問之前初始化'$ scope.payId'。像這樣:'$ scope.payId = 10' –

+0

你可以使用一個表單,然後使用角度驗證:https://docs.angularjs.org/guide/forms –

回答

3

您可以直接使用將包含用戶的$scope.payId輸入的值

DEMO

3

您可以使用對象

$scope.pay = {payId:null}; 


if($scope.pay.payId == null) { 
    console.log("Value not entered"); 
} else { 
     console.log("Value entered"); 
    }