【发布时间】:2020-05-29 11:09:08
【问题描述】:
我正在使用 Angular 9 和 Angular Material 开发联系人应用程序。列表组件应在表格行中为每个联系人显示一些信息和图像。
在我的app.module.ts 中,我导入了MatTableModule 等。
在app\services\contacts-list.service.ts 我有:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Contact } from '../models/Contact';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
@Injectable({
providedIn: 'root'
})
export class ContactsListService {
contactsUrl = 'https://randomuser.me/api/?&results=20&inc=name,location,email,cell,picture';
constructor(private http:HttpClient) { }
getContcts():Observable<Contact[]> {
return this.http.get<Contact[]>(`${this.contactsUrl}`)
.pipe(map(response => response['results']));
}
getOneContact():Observable<Contact[]> {
return this.http.get<Contact[]>(`${this.contactsUrl}`)
.pipe(map(response => response['results']));
}
}
在app\components\list\list.component.ts我有:
import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { from } from 'rxjs';
import { ContactsListService } from '../../services/contacts-list.service';
import { Contact } from '../../models/Contact';
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
})
export class ListComponent implements OnInit {
pageTitle = 'My Contacts';
contactsList:Contact[];
constructor(private ContactsListService:ContactsListService) { }
contactsTableData: MatTableDataSource<any>;
displayedColumns: string[] = ['fullName', 'photo', 'email', 'city', 'country'];
ngOnInit(): void {
// Contact list
this.ContactsListService.getContcts().subscribe(
contactsList => {
this.contactsList = contactsList; console.log(contactsList);
this.contactsTableData = new MatTableDataSource(contactsList);
},
error => { }
);
}
}
我上面组件的模板:
<div class="container">
<h2 class="mat-display-1 page-title">{{pageTitle}}</h2>
<div class="mat-elevation-z8">
<mat-table [data-source]="contactsTableData">
<ng-container matColumnDef="fullName">
<mat-header-cell *matHeaderCellDef> Full Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.name.first}} {{element.name.last}} </mat-cell>
</ng-container>
<ng-container matColumnDef="photo">
<mat-header-cell *matHeaderCellDef> Photo </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.picture.large}} </mat-cell>
</ng-container>
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef> Email address </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.email}} </mat-cell>
</ng-container>
<ng-container matColumnDef="city">
<mat-header-cell *matHeaderCellDef> City </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.location.city}} </mat-cell>
</ng-container>
<ng-container matColumnDef="phone">
<mat-header-cell *matHeaderCellDef> Phone </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.cell}} </mat-cell>
</ng-container>
<ng-container matColumnDef="country">
<mat-header-cell *matHeaderCellDef> Country </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.location.country}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
</div>
</div>
问题
我面临的问题是只显示表头(列名)而没有数据,evan 认为组件中的行 console.log(contactsList) 正确显示了联系人:
[
{
"name":{
"title":"Mr",
"first":"Mair",
"last":"da Cunha"
},
"location":{
"street":{
"number":1198,
"name":"Rua Belo Horizonte "
},
"city":"Cachoeiro de Itapemirim",
"state":"Tocantins",
"country":"Brazil",
"postcode":36034,
"coordinates":{
"latitude":"-78.1101",
"longitude":"-171.0313"
},
"timezone":{
"offset":"+5:00",
"description":"Ekaterinburg, Islamabad, Karachi, Tashkent"
}
},
"email":"mair.dacunha@example.com",
"cell":"(66) 5998-0008",
"picture":{
"large":"https://randomuser.me/api/portraits/men/88.jpg",
"medium":"https://randomuser.me/api/portraits/med/men/88.jpg",
"thumbnail":"https://randomuser.me/api/portraits/thumb/men/88.jpg"
}
}
]
缺少什么?
【问题讨论】:
-
docs 似乎使用
<tr mat-header-row ...></tr>而不是<mat-header-row ...></mat-header-row>? (编辑:忽略我猜,他们也使用th mat-header-cell,但你说你的标题代码确实有效) -
文档似乎也使用
dataSource而不是data-source。您可以尝试更改它。 -
@AkashBhardwaj 确实是这个问题。
-
是的,我在看一个旧的(呃)教程。谢谢!
标签: javascript angular angular-material