简体   繁体   English

Angular 5默认值未默认选中

[英]Angular 5 default value not default selected

When you create a select with ngFor loop for options, default value from ngModel is not selected 使用ngFor循环创建选项时,未选择ngModel的默认值

HTML : HTML:

<form style="margin : 20%;">
    <div class="row">
        <div class="form-group">
            <label for="foo">K1 :{{keyboard | json}}</label>
            <select [(ngModel)]="keyboard" name="foo" class="custom-select"> 
                <option *ngFor="let a of foo" [ngValue]="a">{{a.name}}</option> 
            </select>
        </div>
    </div>
    <div class="row">


        <div class="form-group">
            <label for="fooz">K2 :{{keyboard2 | json}}</label>
            <select [(ngModel)]="keyboard2" name="fooz" class="custom-select"> 
                <option [ngValue]="foo[0]">AZERTY</option> 
                <option [ngValue]="foo[1]">QWERTY</option> 
            </select>
        </div>
    </div>
    <div class="row">


        <div class="form-group">
            <label for="fooa">K2 :{{keyboardStr | json}}</label>
            <select [(ngModel)]="keyboardStr" name="fooa" class="custom-select"> 
                <option [selected]="keyboardStr == 'AZERTY'" [ngValue]="AZERTY">AZERTY</option> 
                <option [selected]="keyboardStr == 'QWERTY'" [ngValue]="QWERTY">QWERTY</option> 
            </select>
        </div>
    </div>
    <div class="row">

        <div class="form-group">
            <label for="fooa">K2-bis :{{keyboardStr | json}}</label>
            <select [(ngModel)]="keyboardStr" name="fooa" class="custom-select"> 
                <option [selected]="keyboardStr == 'AZERTY'" [value]="AZERTY">AZERTY</option> 
                <option [selected]="keyboardStr == 'QWERTY'" [value]="QWERTY">QWERTY</option> 
            </select>
        </div>
    </div>
    <div class="row">

        <div class="form-group">
            <label for="fooa">K2-ter :{{keyboardStr | json}}</label>
            <select [(ngModel)]="keyboardStr" name="fooa" class="custom-select"> 
                <option [selected]="keyboardStr == 'AZERTY'" value="AZERTY">AZERTY</option> 
                <option [selected]="keyboardStr == 'QWERTY'" value="QWERTY">QWERTY</option> 
            </select>
        </div>
    </div>
</form> 

Component : 零件 :

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

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

    private foo: any[] = [{id: 1, name: "AZERTY"}, {id: 2, name: "QWERTY"}];
    private keyboard: any = {id: 1, name: 'AZERTY'};
    private keyboard2 : any = {id: 1, name: 'AZERTY'};
    private keyboardStr : string = 'AZERTY';

  constructor() { }

  ngOnInit() {
  }

}

Result : 结果:

无标题

Shouldn't AZERTY be default selecteded ? AZERTY不应该默认选择吗?

Is there a conflict when using both ngModel and ngValue ? 使用ngModel和ngValue时是否存在冲突? Because in the case of K1 example, value cannot be used as 'a' is an object right ? 因为在K1的例子中,值不能用作'a'是对象吗?

Edit : 编辑:

@Roy D. Porter Ok but imagine that I have this 'unit' object : @Roy D. Porter好的,但想象我有这个'单位'对象:

{
  "id": 1,
  "type": "REMISE",
  "owner": "OWN",
  "to": {
    "id": 1,
    "alone": true,
    "level": 1,
    "name": "Participant",
    "minSales": 0.0,
    "minTeamNumber": 0,
    "durationCondition": 0,
    "durationAwardCondition": null
  },
  "limit": 0.0,
  "percentage": 25.0,
  "alone": true
}

This will display well the type as model is a string : 这将很好地显示类型,因为model是一个字符串:

<select [(ngModel)]="unit.type" name="tye" class="custom-select">
                    <option *ngFor="let type of types" [ngValue]="type">{{type | camelCase}}</option>
                </select>

This does not display the default value it should : {{award.name | 这不显示它应该的默认值:{{award.name | camelCase}} 骆驼香烟盒}}

I assume this is caused by award not being a string. 我认为这是因为奖励不是一个字符串。 From what I see, ngModel is selected when it is a string but not when it's an object. 从我看到的,当它是一个字符串时,选择ngModel,而当它是一个对象时,不选择它。

Angular v5.0.0 Angular v5.0.0

I've faced the same issue as you. 我遇到了和你一样的问题。

My usual solution is to add an option to the list, and set it's value to -1, and initialise the binded variable to -1. 我通常的解决方案是在列表中添加一个选项,并将其值设置为-1,并将绑定变量初始化为-1。

So in your case: 所以在你的情况下:

        <select [(ngModel)]="keyboard" name="foo" class="custom-select">
            <option value="-1">Please select an answer</option>
            <option *ngFor="let a of foo" [ngValue]="a">{{a.name}}</option> 
        </select>

And in your component controller, initialise the keyboard variable to -1. 在组件控制器中,将keyboard变量初始化为-1。

When you use [ngValue] on option, use [compareWith] on select. 当您在选项上使用[ngValue]时,请在选择时使用[compareWith]。 Give it a function that compare two objects (in type of your ngValue and ngModel objects). 给它一个比较两个对象的函数(在ngValue和ngModel对象的类型中)。

You have examples in the doc here : https://angular.io/api/forms/SelectControlValueAccessor 您在此处的文档中有示例: https//angular.io/api/forms/SelectControlValueAccessor

Best regards 最好的祝福

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM