【问题标题】:Angular 2, set default value to select optionAngular 2,设置默认值以选择选项
【发布时间】:2017-02-05 01:20:29
【问题描述】:

我尝试为选项添加默认值。它就像一种占位符,我使用this method 来完成它。 在纯 HTML 中它可以工作,但如果我尝试使用 angular 2 属性的 *ngFor,它不会选择任何东西。

这是我在纯 HTML 中使用的代码:

  <select name="select-html" id="select-html">
    <option value="0" selected disabled>Select Language</option>
    <option value="1">HTML</option>
    <option value="2">PHP</option>
    <option value="3">Javascript</option>
  </select>

并使用 Angular 模板:

 <select name="select" id="select" [(ngModel)]="selectLanguage">
    <option *ngFor="let item of selectOption" 
      [selected]="item.value==0" 
      [disabled]="item.value==0">{{item.label}}</option>
  </select>

班级是

import {Component} from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'app-form-select',
  templateUrl: 'default.component.html'
})
export class DefaultComponent {
  private selectOption: any;
  constructor() {
    this.selectOption = [
      {
        id: 1,
        label: "Select Language",
        value: 0
      }, {
        id: 2,
        label: "HTML 5",
        value: 1
      }, {
        id: 3,
        label: "PHP",
        value: 2
      }, {
        id: 4,
        label: "Javascript",
        value: 3
      }
    ]
  }
}

我希望Select Language被选为默认值。它被禁用但未被选中。

如何选择它作为默认值?

Live example

【问题讨论】:

    标签: javascript angular angular2-forms


    【解决方案1】:
    <select class="form-control" [(ngModel)]="someList.key" name="key"  #key="ngModel" required>
         <option value="undefined" selected disabled>Select option</option>
         <option *ngFor="let data of someList" value="{{data.key}}">{{data.key}</option>
     </select>
    

    【讨论】:

      【解决方案2】:

      我发现从selectOption 数组中删除默认选项并单独指定默认的不可选择选项会更好。

      <select name="select" id="select" [(ngModel)]="selectLanguage">
        <option [ngValue]="undefined" disabled>Select Language</option>
        <option
               *ngFor="let item of selectOption"
               [value]="item.value"
        >
          item.label
        </option>
      </select>
      

      【讨论】:

      • 为什么这不是公认的答案。另一个答案非常复杂。
      • 这是我一直在寻找的绝对答案。当您的选项动态来自服务时,当您想要选择“Select blah blah”等默认选项时,这很有用。
      【解决方案3】:

      试试下面的示例代码:
      将yourValue 替换为您想要选择默认值的任何值

      <select  placeholder="country" >
             <option *ngFor="let country of countriesList" [ngValue]="country" [selected]="country.country_name == yourValue" >
                   {{country.country_name}}
             </option>
      </select>
      

      【讨论】:

        【解决方案4】:

        更新

        4.0.0-beta.6 添加了compareWith

        <select [compareWith]="compareFn"  [(ngModel)]="selectedCountries">
            <option *ngFor="let country of countries" [ngValue]="country">
                {{country.name}}
            </option>
        </select>
        
        compareFn(c1: Country, c2: Country): boolean {
           return c1 && c2 ? c1.id === c2.id : c1 === c2;
        } 
        

        这样分配给selectedCountries的实例不需要是相同的对象,但可以传递自定义比较函数,例如能够匹配具有相同属性值的不同对象实例。

        原创

        如果你想使用一个对象作为值,你需要在选项元素上使用[ngValue]。

          <select name="select" id="select" [(ngModel)]="selectLanguage">
            <option *ngFor="let item of selectOption" [ngValue]="item"
              [disabled]="item.value==0">{{item.label}}</option>
          </select>
        

        当selectLanguage 分配了一个选项值[(ngModel)]="..." 时,此选项是否会默认选中:

        import {Component} from '@angular/core';
        
        @Component({
          moduleId: module.id,
          selector: 'app-form-select',
          templateUrl: 'default.component.html'
        })
        export class DefaultComponent {
          private selectOption: any;
          constructor() {
            this.selectOption = [
              {
                id: 1,
                label: "Select Language",
                value: 0
              }, {
                id: 2,
                label: "HTML 5",
                value: 1
              }, {
                id: 3,
                label: "PHP",
                value: 2
              }, {
                id: 4,
                label: "Javascript",
                value: 3
              }
            ];
            this.selectLanguage = this.selectOption[0];
          }
        }
        

        【讨论】:

        • @CTN 对不起,我不明白你的问题。什么标签和什么 HTML DOM?
        • 我想将html标签设置为标签值。
        • 如果您正在寻找 Angular 11 解决方案。答案在这里stackoverflow.com/a/66229168/9207553
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-13
        • 2018-06-22
        • 2015-09-19
        • 1970-01-01
        • 2016-06-28
        相关资源
        最近更新 更多