2017-01-17 8 views
1

私は角度jsドロップダウンを持っています。私は私のvariabl場合、それが欲しい。 'x'は値trueを持ちます。ドロップダウンから値を選択することを強制し、ドロップ値から何も選択せずに値を保存することができるfalse値を入れます。ここでダウン私のドロップされます。変数がtrueの場合、ドロップダウン選択を強制的に行う方法はありますか?

<select class="form-control dropdownheight" 
     ng-model="search.hardware" 
     ng-change="search()" > 
    <option value="" selected>Select Hardware</option> 
    <option ng-repeat="box in boxes" value="{{box.name}}">{{box.name}}</option> 
</select> 

答えて

1

を参照してください。

 <form name="form"> 
    <select ng-model="hardware" class="form-control dropdownheight" ng-change="search()" > 
<option value="" selected>Select Hardware</option> 
<option ng-repeat="box in boxes" value="{{box.name}}">{{box.name}}</option> 
           </select> 

    <label for="input">This input must be filled if `required` is true: </label> 
    <input type="text" id="input" name="input" ng-required="isRequired" /><br> 
    <hr> 
    <code>{{form.input.$error}}</code><br> 
    isRequired = <code>{{isRequired}}</code> 

    <input type="submit" value="Save" ng-disabled="isRequired"/> 
    </form> 

私が作成したplunkrそれのために:

https://plnkr.co/edit/bfWuGCOYLGIV0Krlaaeo?p=preview

+0

ありがとう:)私はそれを修正し、必要な機能を取得するための関数を作りました。ご協力いただきありがとうございます – Ali

1

ng-required directiveを使用します。

<select class="form-control dropdownheight" 
     ng-model="search.hardware" 
     ng-change="search()" 
     ng-required="x"> 
    <option value="" selected>Select Hardware</option> 
    <option ng-repeat="box in boxes" value="{{box.name}}">{{box.name}}</option> 
</select> 

は、あなたがこのために必要なNG使用することができますthis jsfiddle

関連する問題