【发布时间】:2020-10-21 22:37:30
【问题描述】:
我有一个页面,其中列出了我所有的注册用户,我想创建一个操作,在其中单击用户名并打开另一个组件以显示来自此特定用户的信息。我已经创建了另一个名为 user-details 的组件。如何做到这一点的最佳方法?也许从 URL 读取 ID?
这是我的代码:
users.component.ts
import { Component, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
@Component({
selector: 'app-manage-users',
templateUrl: './manage-users.component.html',
styleUrls: ['./manage-users.component.scss']
})
export class ManageUsersComponent implements OnInit {
items: Observable<any[]>;
constructor(firestore: AngularFirestore) {
this.items = firestore.collection('users').valueChanges();
}
ngOnInit(): void {
}
}
users.component.html
<div class="card card-custom example example-compact gutter-b">
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Actions</th>
<th scope="col">Sign-up Time</th>
<th scope="col">Username</th>
<th scope="col">Email</th>
<th scope="col">Is Enabled</th>
<th scope="col">Is Online</th>
<th scope="col">Last Time Online</th>
<th scope="col">Age</th>
<th scope="col">Gender</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of items | async">
<td>
<div ngbDropdown container="body">
<button class="btn" ngbDropdownToggle>Actions</button>
<div ngbDropdownMenu>
<button ngbDropdownItem>View</button>
<button ngbDropdownItem>Block</button>
<div class="dropdown-divider"></div>
<button ngbDropdownItem>Delete</button>
</div>
</div>
</td>
<td>{{item.createdDate}}</td>
<td><a target="_blank" href="/user-details">{{item.name}}</a></td>
<td>{{item.email}}</td>
<td>{{item.isEnabled}}</td>
<td>{{item.online}}</td>
<td>{{item.last_time_online}}</td>
<td>{{item.birthday}}</td>
<td>{{item.gender}}</td>
</tr>
</tbody>
</table>
</div>
</div>
【问题讨论】:
-
您需要将唯一标识符传递给用户详细信息页面。前任。 href="/user-details/{{item.id}}"
-
您可以通过多种方式做到这一点,
i) pass in urlii) set on click in the localStorageiii) use @Input to pass the id to the child component.
标签: angular typescript firebase-realtime-database