bootstrap-select selects a wrong option each time.

点点圈 提交于 2019-12-12 06:12:16

问题


I am using bootstrap select Bootstrap-select v1.7.2 in Angular. When I select some option from drop down it selects some other option.

Plunker is setup here:

http://plnkr.co/edit/HPPlxx?p=preview

(code is in dashboard.html and dashboard.js)

That's how I am creating it. bootstrap-dropdown is the directive that initiates the drop-down.

  <form role="form" name="frmVariableConfig" ng-submit="vm.saveChanges()">
        <select ng-model="vm.CurrCustomer.Logic" name="ddlLogic" check-validation class="validate[required]" bootstrap-dropdown >
           <option ng-repeat="logic in vm.Logics" value="{{logic.value}}">{{logic.displayName}}</option>
        </select>
        <button type="submit" class="btn btn-sm text-right">Save</button>
     </form>

回答1:


This should fix your problem. You need a blank state option. Also it helps to use ng-options rather than using ng-repeat in the option. Hope this solves your problem!

<div ng-controller="dashboard as vm">
   <div class="block-area">
      <div class="row col-md-12 ">
         <h3 class="block-title">  {{vm.title}}</h3>
         <form role="form" name="frmVariableConfig" ng-submit="vm.saveChanges()">
            <select required ng-model="vm.CurrCustomer.Logic" name="ddlLogic" check-validation class="validate[required]" ng-options="logic.displayName for logic in vm.Logics track by logic.displayName" bootstrap-dropdown >
               <option value="">Pick One</option>
            </select>
            <button type="submit" class="btn btn-sm text-right">Save</button>
         </form>
      </div>
   </div>
</div>



回答2:


You should use ng-options to populate the option elements of a select input.

Like so:

<select ng-model="vm.CurrCustomer.Logic" data-ng-options="logic.displayName for logic in vm.Logics track by logic.value" name="ddlLogic" check-validation class="validate[required]" bootstrap-dropdown >
    <option value="">Select...</option>
</select>

EDIT

Use the following:

<select ng-model="vm.CurrCustomer.Logic" data-ng-options="logic.displayName for logic in vm.Logics" check-validation class="validate[customFunc]" bootstrap-dropdown>
         <option style="display:none" value="">Select</option>
</select>

where customFunc is a new custom validation function you create to check that the selected value is not "".



来源:https://stackoverflow.com/questions/31016810/bootstrap-select-selects-a-wrong-option-each-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!