简体   繁体   English

不显示 select 属性的选定值。 打字稿/角度

[英]The selected value of the select attribute is not displayed. TypeScript/Angular

I don't see anything in the item selection window. The elements are loaded from the Web APi server.我在项目选择window中没有看到任何内容。元素是从Web APi服务器加载的。 Here is the photo of problem .这是问题的照片

Component class组件 class

carsDynamic!: Array<Car>

private loadDynamicData(){
  this.dynamic.getDynamicCars().subscribe((data: Car[]) => {
  this.carsDynamic = data
})

HTML page HTML 页

<select  class="form-control" [(ngModel)]="editedSale.carId">
  <option disabled selected>--Select--</option>  
  <option *ngFor="let car of carsDynamic"  
          value={{car.id}}>  
          {{car.brandName}}  
  </option>  
</select>

How to fix this problem?如何解决这个问题?

Problem originates from [(ngModel)]="editedSale.carId" .问题源于[(ngModel)]="editedSale.carId" You have to choose, either template driven from or reactive form.你必须选择,要么是模板驱动,要么是反应形式。

@Component({
  selector: 'select-reset-example',
  templateUrl: 'select-reset-example.html',
  styleUrls: ['select-reset-example.css'],
})
export class SelectResetExample {
  dynamic = new ReplaySubject<any>(1);
  carsDynamic: any[];
  data = [
    { id: 1, brandName: 'Audi' },
    { id: 2, brandName: 'BMW' },
  ];

  constructor() {
    this.loadDynamicData();
    this.dynamic.next(this.data);
  }

  private loadDynamicData() {
    this.dynamic.subscribe((data: Car[]) => {
      this.carsDynamic = data;
    });
  }

  onSelect(event: any) {
    console.log(event.target.value);
  }
}

export interface Car {
  id: number;
  brandName: string;
}

<select class="form-control" (change)="onSelect($event)">
  <option disabled selected>--Select--</option>
  <ng-container *ngFor="let car of carsDynamic">
    <option value="{{car | json}}">{{car.brandName}}</option>
  </ng-container>
</select>

<select class="form-control" (change)="onSelect($event)">
  <option disabled selected>--Select--</option>
  <ng-container *ngFor="let car of carsDynamic">
    <option value="{{car.id}}">{{car.brandName}}</option>
  </ng-container>
</select>

<select class="form-control" (change)="onSelect($event)">
<option disabled selected>--Select--</option>
<ng-container *ngFor="let car of carsDynamic">
  <option value="{{car.brandName}}">{{car.brandName}}</option>
</ng-container>
</select>

Look into console log.查看控制台日志。 Working example: https://stackblitz.com/edit/angular-material-matselect-ybbg2t?file=app%2Fselect-reset-example.ts工作示例: https://stackblitz.com/edit/angular-material-matselect-ybbg2t?file=app%2Fselect-reset-example.ts

I found the cause of the malfunction.我找到了故障原因。 The fact is that I downloaded the wrong data model from the server事实是我从服务器下载了错误的数据model

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

相关问题 根据选择添加所需的属性 <select>值 - Add required attribute based on selected <select> value 在选择选项中选择给出错误的属性值 - selected in select option give wrong attribute value Select 选项按值和更改所选选项的属性 - Select option by value and change attribute of a selected option 使用HTML时 <select>标记,更改了Firefox中未显示的“已选择”值 - When using HTML <select> tag, changed 'selected' value not displayed in Firefox Angular 8:为 select 标签设置默认选择值 - Angular 8 : Set default selected value for select tag 具有显示检索到的值的ascx控件。 想要使用文本输入框作为值更改发回 - Have an ascx control with retrieved values displayed. Want to use a text input box to post back as value changes 在未显示可排序值的列上对HTML表进行排序。 任何现有的解决方案? - Sorting a HTML table on a column whose sortable value is not displayed. Any existing solutions? 选定值未显示在Rails中 - Selected Value Not Displayed in Rails Angular 7根据传递的数据选择选定的属性选项 - Angular 7 select selected attribute option based on passed data 如何基于选择的选择选项动态更改输入值属性? - How to change dynamically input value attribute based on select option selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM