Ionic/Angular 如何向列表添加数据
Ionic/Angular How to add data to list
我有一个正在开发的应用程序,目前我正在开发一个部分,用户可以在该部分中点击标有 'Emergency Contacts' 的项目。然后向用户呈现一个包含 5 个空块的列表,每个块都有一个标签
姓名:
人数:
用户点击一个块,然后select一个联系人。
目前,我可以使用联系人列表中用户 select 的姓名和号码填充其中一个块。
这里是相关代码
import { Component, OnInit } from '@angular/core';
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts/ngx';
@Component({
selector: 'app-contact-component',
templateUrl: './contact-component.component.html',
styleUrls: ['./contact-component.component.scss'],
})
export class ContactComponentComponent implements OnInit {
constructor(private contacts: Contacts) { }
ngOnInit() {}
cName:any;
cNumber:any;
pickContact() {
this.contacts.pickContact().then((contact) => {
this.cName = contact.name.givenName;
this.cNumber = contact.phoneNumbers[0].value;
// console.log(cNumber);
});
}
}
这是hmtl
重复 5 次得到 5 个方块
<ion-grid>
<ion-row>
<ion-col>
<ion-item-group (click) = "pickContact()">
<ion-card>
<ion-item lines = "none">
<ion-label class="ion-text-wrap">Name: {{cName}}</ion-label>
</ion-item>
<ion-item lines = "none" >
<ion-label class="ion-text-wrap">Number: {{cNumber}}</ion-label>
</ion-item>
</ion-card>
</ion-item-group>
我的问题是我不知道如何在没有成堆代码的情况下重复这个。
我正在考虑使用嵌套数组,但我完全不确定如何去做
我希望用户点击一个块 -> select 一个联系人 -> 函数填充相应的块。
有什么建议吗?
Angular 在处理列表方面表现出色。实际上,您不必对整个列表进行硬编码。
你需要的是 *ngFor 指令。
.html 文件
<ion-card *ngFor="let contact of emergencyContacts; let i=index">
<ion-item-group (click)="pickContact(i)">
<ion-item lines = "none">
<ion-label class="ion-text-wrap">Name: {{contact.name}}</ion-label>
</ion-item>
<ion-item lines = "none" >
<ion-label class="ion-text-wrap">Number: {{contact.number}}</ion-label>
</ion-item>
</ion-item-group>
</ion-card>
.ts 文件
export class ContactComponentComponent implements OnInit {
/*
of course, the following array would be better to be created by a loop
I leave it this way to be easier to understand
*/
emergencyContacts = [
{name: '', number: ''},
{name: '', number: ''},
{name: '', number: ''},
{name: '', number: ''},
{name: '', number: ''}
]
constructor(private contacts: Contacts) { }
ngOnInit() {}
pickContact(i) {
this.contacts.pickContact().then((contact) => {
this.emergencyContacts[i].name = contact.name.givenName;
this.emergencyContacts[i].number = contact.phoneNumbers[0].value;
});
}
}
我有一个正在开发的应用程序,目前我正在开发一个部分,用户可以在该部分中点击标有 'Emergency Contacts' 的项目。然后向用户呈现一个包含 5 个空块的列表,每个块都有一个标签 姓名: 人数:
用户点击一个块,然后select一个联系人。
目前,我可以使用联系人列表中用户 select 的姓名和号码填充其中一个块。
这里是相关代码
import { Component, OnInit } from '@angular/core';
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts/ngx';
@Component({
selector: 'app-contact-component',
templateUrl: './contact-component.component.html',
styleUrls: ['./contact-component.component.scss'],
})
export class ContactComponentComponent implements OnInit {
constructor(private contacts: Contacts) { }
ngOnInit() {}
cName:any;
cNumber:any;
pickContact() {
this.contacts.pickContact().then((contact) => {
this.cName = contact.name.givenName;
this.cNumber = contact.phoneNumbers[0].value;
// console.log(cNumber);
});
}
}
这是hmtl 重复 5 次得到 5 个方块
<ion-grid>
<ion-row>
<ion-col>
<ion-item-group (click) = "pickContact()">
<ion-card>
<ion-item lines = "none">
<ion-label class="ion-text-wrap">Name: {{cName}}</ion-label>
</ion-item>
<ion-item lines = "none" >
<ion-label class="ion-text-wrap">Number: {{cNumber}}</ion-label>
</ion-item>
</ion-card>
</ion-item-group>
我的问题是我不知道如何在没有成堆代码的情况下重复这个。
我正在考虑使用嵌套数组,但我完全不确定如何去做 我希望用户点击一个块 -> select 一个联系人 -> 函数填充相应的块。
有什么建议吗?
Angular 在处理列表方面表现出色。实际上,您不必对整个列表进行硬编码。
你需要的是 *ngFor 指令。
.html 文件
<ion-card *ngFor="let contact of emergencyContacts; let i=index">
<ion-item-group (click)="pickContact(i)">
<ion-item lines = "none">
<ion-label class="ion-text-wrap">Name: {{contact.name}}</ion-label>
</ion-item>
<ion-item lines = "none" >
<ion-label class="ion-text-wrap">Number: {{contact.number}}</ion-label>
</ion-item>
</ion-item-group>
</ion-card>
.ts 文件
export class ContactComponentComponent implements OnInit {
/*
of course, the following array would be better to be created by a loop
I leave it this way to be easier to understand
*/
emergencyContacts = [
{name: '', number: ''},
{name: '', number: ''},
{name: '', number: ''},
{name: '', number: ''},
{name: '', number: ''}
]
constructor(private contacts: Contacts) { }
ngOnInit() {}
pickContact(i) {
this.contacts.pickContact().then((contact) => {
this.emergencyContacts[i].name = contact.name.givenName;
this.emergencyContacts[i].number = contact.phoneNumbers[0].value;
});
}
}