如果变量为true,如何强制下拉选择?

时间:2017-01-17 11:31:08

标签: javascript angularjs dropdown

我有一个角度js下拉列表。我希望如果我的变量,例如'x'的值为true它使得必须从下拉列表中选择一个值,并允许它保存它而不从下拉列表中选择任何内容。这是我的下拉:

WKInterfaceGroup

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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