2017-01-17 1 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

당신이 필요 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