简体   繁体   English

显示angular中的数组数据

[英]display array data in angular

i'm trying to learn angular after a long time with react and i want to display data from an array.经过很长时间的反应,我正在尝试学习 angular,我想显示数组中的数据。

I want to use a dropdown menu to select the id and to display the address of the selcted id.我想使用下拉菜单 select id 并显示 selcted id 的地址。

The select code should look like this i guess我猜 select 代码应该是这样的

<select name="customer" >
  <option *ngFor='let customer of customers'>
    {{ customer.id }}
  </option>
</select>

Here is a array example这是一个数组示例

export interface Customer {
  id: number,
  companyName: string,
  firstName: string,
  lastName: string,
  street: string,
  streetNumber: number,
  zipCode: number,
  city: string,
  country: string,
}

export const customers = [
  {
    id: 800001,
    companyName: 'example company 1',
    firstName: 'John',
    lastName: 'Doe',
    street: 'Example Street',
    streetNumber: 7,
    zipCode: 11111,
    city: 'example city',
    country: 'example country'
  }
  {
    id: 800002,
    companyName: 'example company 2',
    firstName: 'Jessica',
    lastName: 'Doe',
    street: 'Example Street',
    streetNumber: 8,
    zipCode: 11111,
    city: 'example city',
    country: 'example country'
  }
  {
    id: 800003,
    companyName: 'example company 3',
    firstName: 'Mark',
    lastName: 'Doe',
    street: 'Example Street',
    streetNumber: 69,
    zipCode: 11111,
    city: 'example city',
    country: 'example country'
  }
]

In angular, to use variables in.ts in the.html we use, see the docs :在 angular 中,要在我们使用的 .html 中使用变量 in.ts,请参阅文档

  1. Interpolation, eg插值,例如

    {{variable}}
  2. Binding syntax,eg绑定语法,例如

    <input [value]="variable">
  3. Two way binding,eg双向绑定,例如

    <input [(ngModel)]="variable">
  4. Built-in-directives (*ngFor, *ngIf...),eg内置指令(*ngFor、*ngIf...),例如

    <div *ngFor="let item of [0,1,2,3]>{{item}}</div>
  5. Others object like Reactive Forms其他 object 喜欢 Reactive Forms

  6. ... ...

I can say the response to your question我可以回答你的问题

<select [(ngModel)]="customer" >
  <option *ngFor='let item of customers' [ngValue]="item">
    {{ customer.id }}
  </option>
</select>
{{customer|json}}

But I recommended, make a tour of heroes to learn how Angular works (You can skip the animations parts -for me the more complex-)但我建议,参观英雄以了解 Angular 的工作原理(您可以跳过动画部分 - 对我来说更复杂 - )

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

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