Angular 当页面加载时,应该已经选择了第一个条目并显示其内容,如何做到这一点?

Angular 当页面加载时,应该已经选择了第一个条目并显示其内容,如何做到这一点?,angular,dropdown,Angular,Dropdown,我创建了一个ngModel和ngchange,它绑定到ts文件中的json数组并显示其内容 当页面加载时,应该已经选择了第一个条目并显示其内容。我想在选择默认下拉列表值并显示其内容之前显示它。到目前为止,加载时只有一个空白屏幕,当我选择下拉菜单时,就会加载内容 我试着先显示默认内容,然后如果用户单击它,它就会改变 App.component.html ''' <div> Select Product : <select [(ngModel)]="Pro

我创建了一个ngModel和ngchange,它绑定到ts文件中的json数组并显示其内容

当页面加载时,应该已经选择了第一个条目并显示其内容。我想在选择默认下拉列表值并显示其内容之前显示它。到目前为止,加载时只有一个空白屏幕,当我选择下拉菜单时,就会加载内容

我试着先显示默认内容,然后如果用户单击它,它就会改变

App.component.html

'''
<div>
    Select Product :
    <select [(ngModel)]="ProductHeader.name" (ngModelChange)="SearchProduct(ProductHeader.name)">
        <option *ngFor="let prod of ProductHeader">{{prod.name}} </option>
    </select>



    <div *ngIf="ProductHeader.name">
        <h5>You have selected: {{ProductHeader.name}} Product </h5>
    </div>
</div>
<div>
    <h4>Product Details:</h4>
    <table class="TFtable">
        <tr>
            <th>Name</th>
            <th>Store</th>
            <th>Price</th>
            <th>Model</th>
        </tr>
        <tr *ngFor="let Prod of ProductDetails">
            <td>{{Prod.title}}</td>
            <td>{{Prod.store}}</td>
            <td>{{Prod.price}}</td>
            <td>{{Prod.model}}</td>
        </tr>

'''

很抱歉,为了使代码正常工作,我不得不对代码进行一些重构

HTML

提示1

当您使用
[(ngModel)]
时,将其连接到一个单独的变量,您可以在该变量中进行读取和写入。我为此添加了
selectedProductModel
。当您现在选择一个ProductModel时,此对象将被放入
selectedProductModel
中,然后您可以调用
searchProduct()
,而无需提交任何其他值

提示2

select中的选项必须具有值才能正常工作。这就是我添加
[ngValue]
的原因

这是您的精制HTML

<div>
  Select Product :
  <select [(ngModel)]="selectedProductModel" (ngModelChange)="searchProduct()">
    <option *ngFor="let productModel of productModels" [ngValue]="productModel">{{productModel.name}}</option>
  </select>
  <div *ngIf="selectedProductModel && selectedProductModel.name.length > 0">
    <h5>You have selected: {{selectedProductModel.name}} Product </h5>
  </div>
</div>
<div>
  <h4>Product Details:</h4>
  <table class="TFtable">
    <tr>
      <th>Name</th>
      <th>Store</th>
      <th>Price</th>
      <th>Model</th>
    </tr>
    <tr *ngFor="let prod of productDetails">
      <td>{{prod.title}}</td>
      <td>{{prod.store}}</td>
      <td>{{prod.price}}</td>
      <td>{{prod.model}}</td>
    </tr>
  </table>
</div>

我想在选择它之前显示默认下拉列表值并显示其内容
这是否意味着在页面加载时应该已经选择了第一个条目并显示了其内容?是的,应该是在页面加载时已经选择了第一个条目并显示了其内容。单击下拉列表后,内容可以更改。
<div>
  Select Product :
  <select [(ngModel)]="selectedProductModel" (ngModelChange)="searchProduct()">
    <option *ngFor="let productModel of productModels" [ngValue]="productModel">{{productModel.name}}</option>
  </select>
  <div *ngIf="selectedProductModel && selectedProductModel.name.length > 0">
    <h5>You have selected: {{selectedProductModel.name}} Product </h5>
  </div>
</div>
<div>
  <h4>Product Details:</h4>
  <table class="TFtable">
    <tr>
      <th>Name</th>
      <th>Store</th>
      <th>Price</th>
      <th>Model</th>
    </tr>
    <tr *ngFor="let prod of productDetails">
      <td>{{prod.title}}</td>
      <td>{{prod.store}}</td>
      <td>{{prod.price}}</td>
      <td>{{prod.model}}</td>
    </tr>
  </table>
</div>
import { Component, Input, OnInit } from '@angular/core';

// an object that represents a product detail.
class ProductDetails {
  constructor(
    public name: string,
    public title: string,
    public price: string,
    public store: string,
    public model: string
  ) {
  }
}

// an object that represents the product model
class ProductModel {
  constructor(
    public name: string
  ) {
  }
}

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

  // a list of product models
  public productModels: Array<ProductModel> = [
    new ProductModel('Hp'),
    new ProductModel('Dell'),
    new ProductModel('Lenovo')];

  // the currently selected product model. 
  // It is preset with the first product model of the list.
  selectedProductModel = this.productModels[0];

  // the list of product details. It gets filled by the `searchProduct()` method.
  productDetails: any[] = [];

  stringifiedData: any;
  data: any;
  stringData: any;
  parsedJson: any;

  // your list of product details
  public products = [
    new ProductDetails('Hp', 'HP ENVY Laptop - 15t touch', '1099', 'Best Buy', '15-BS013DX'),
    new ProductDetails('Dell', 'Dell Laptop', '700', 'Amazon', 'I7378-3000SLV-PUS'),
    new ProductDetails('Lenovo', 'Lenovo Touch-Screen Laptop', '670', 'Target', '81A40025US'),
    new ProductDetails('Hp', 'HP OfficeJet Pro 6978 All-in-One Printer', '100', 'Target', 'T0F29A#B1H'),
    new ProductDetails('Hp', 'HP Laptop - 17t touch ', '420', 'Target', '1EZ78AV_1'),
    new ProductDetails('Dell', 'Dell - XPS 27" Touch-Screen All-In-One', '670', 'Target', 'BBY-311C3FX'),
    new ProductDetails('Dell', 'Dell - Inspiron 21.5" Touch-Screen All-In-One', '469.90', 'Target', 'I3265-A067BLK-PUS'),
    new ProductDetails('Lenovo', 'Lenovo - 520-24AST 23.8" Touch-Screen All-In-One', '679.99', 'Target', 'F0D3000EUS'),
    new ProductDetails('Dell', 'Dell - XPS 2-in-1 13.3" Touch-Screen Laptop', '1199.99', 'Target', 'XPS9365-7086SLV-PUS')
  ];

  constructor() {
  }

  ngOnInit(): void {
    this.stringifiedData = JSON.stringify(this.products);
    console.log('With Stringify :', this.stringifiedData);

    // Parse from JSON
    this.parsedJson = JSON.parse(this.stringifiedData);
    console.log('With Parsed JSON :', this.parsedJson);

    // fire the search method. As productDetails is already preset with the 
    // first product model, your drowpdown has a selection and the result is shown.
    // this is what you were looking for.
    this.searchProduct();
  }

  searchProduct(): void {
    this.productDetails = this.products.filter(productDetail => productDetail.name === this.selectedProductModel.name);
  }

}