2017-01-17 20 views
1

我有一個角度js下拉列表。我希望如果我的變量例如'x'的值爲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

可以使用NG需要這個。

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

this jsfiddle

相關問題