我目前正在嘗試使用AngularJS驗證一張卡片。直到用戶從輸入字段刪除所有輸入的數據也能正常工作的話,那將引發以下錯誤:AngularJS在獲取空輸入長度時拋出錯誤
angular.js:11004 undefined is not an object (evaluating '$scope.card.length')
我的HTML代碼:
拋出錯誤<input type="text" class='form-control' id='card' ng-model="card" ng-change="validateCard()" required>
JavaScript代碼,當輸入空:
$scope.validateCard = function() {
// Get card type
if ($scope.card.length) { // <-- throws the error
[...]
我也試過
if ($scope.card.length !== undefined) {
但它沒有幫助。
'$ scope.card'也可能是未定義的。 'if($ scope.card && $ scope.card.length)''也許? – tanmay
'if($ scope.card && $ scope.card.length)...' –