【问题标题】:AngularJS - Set the true or false or select one optionAngularJS - 设置真假或选择一个选项
【发布时间】:2021-02-13 15:31:39
【问题描述】:

我想设置我选择的任何内容,我想将其保存到 viewmodel,但我不知道如何将我选择的选项设置为 ng-model。如果未选择任何值,则将选择一个。可用选项是 YES (true) / NO (false)。谢谢大家

<select automationid="APR" name="cost"
                            class="ddl"
                            ng-model="tempDefaultCost.cost"
                            ng-options="item.value as item.text for item in costFlyoutCtrl.trueFalseLookup">
                        <option value="">Select One</option>
</select>

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    如果我正确理解了您的问题,您可以添加一个额外的选项来选择框,即disabledselected

    ng-repeat:

    <select automationid="APR" name="cost" class="ddl" ng-model="$ctrl.selectedItem">
           <option value="" disabled selected>Select one</option>
           <option ng-repeat="x in $ctrl.items">{{x.label}}</option>
    </select>
    

    还有ng-options:

    <select automationid="APR" name="cost"
                                class="ddl"
                                ng-model="$ctrl.selectedItem"
                                ng-options="x.label for x in $ctrl.items">
                            <option value="" disabled selected>Select one</option>
    </select>
    

    在你的控制器中:

    this.items = [{value: true, label: 'TRUE'}, {value: false, label: 'FALSE'}];
    this.selectedItem;
    

    DEMO

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2014-11-18
      • 1970-01-01
      相关资源
      最近更新 更多