2014-11-03 47 views
1

我有一個ng-repeater,每行有一個輸入類型複選框,以及3個文本字段。我想只允許用戶在3個文本字段中的每一箇中輸入一些文本時選中該複選框。如果用戶嘗試選擇複選框而不先輸入數據,我想顯示一條警告消息。有沒有辦法讓一個複選框只有在輸入數據時才被標記?

我假設我必須做一些檢查三個ng模型(對於文本字段)是null或undefined或者什麼的,但不知道如何在HTML中做到這一點。

我的HTML看起來是這樣的:

<div ng-repeat="o in objects"> 
    <input type="checkbox" class="myClass" ng-click="doSomething(argument)> 
    .... 
    .... 
    <input ng-model="model1"> 
    <input ng-model="model2"> 
    <input ng-model="model3"> 

編輯:在這裏找到AngularJS - Form Custom Validation - Check if at least one input is empty

回答

1

答案您可以禁用它們,並使用ng-change來監視的文本輸入的所有3纔有價值。

樣本HTML:

<input type="text" ng-model="data.item3" ng-change="update()"/> 

<input type="checkbox" ng-model="data.model1" ng-disabled="checksDisabled"/> 

示例控制器

.controller('MainCtrl',function($scope){ 
    $scope.checksDisabled=true; 
    var data = $scope.data ={ }; 
    $scope.update=function(){ 
     $scope.checksDisabled = !(data.item1 && data.item2 && data.item3); 
    }   
}); 

DEMO

+0

感謝您的回答,我決定去與我貼在我的編輯路線,因爲它的更少的代碼,但如果出現問題,我可以使用它作爲備份:)。 – gallly 2014-11-03 23:38:02

+0

明白,我個人更喜歡在HTML中少廢話,也更容易測試變量 – charlietfl 2014-11-03 23:38:46

+0

是啊,你可能是正確的,是在做更多的稅收檢查或在這種情況下,這是沒有問題?正在檢查從ng禁用的checksDisable更有效嗎?或者只是更清潔 – gallly 2014-11-03 23:42:30

相關問題