【发布时间】:2022-01-17 12:55:52
【问题描述】:
我有一个 firebase 变量集合,每个变量都是用户独有的。我使用 ngFor 循环将这些变量作为卡片显示给用户。用户可以单击卡片上的按钮来访问有关该变量的更多信息(通过被重定向到该变量唯一的页面)。我的问题在于按钮功能:因为按钮是通过在组件的 HTML 文件中循环显示的,我如何访问组件的打字稿文件中单击的任何特定按钮(及其变量)?我是 Angular 的初学者,所以我不确定这是否是正确的方法。
ngFor 循环:
<div fxLayout="row wrap">
<div class = 'content' *ngFor="let card of cards">
<mat-card>
<mat-card-content>
<mat-card-title>
<h4>{{card.spaceName}}</h4>
</mat-card-title>
<mat-card-actions>
<button mat-raised-button>View Space</button>
</mat-card-actions>
</mat-card-content>
</mat-card>
</div>
</div>
打字稿文件:
interface Space {
}
@Component({
selector: 'app-viewspaces',
templateUrl:'./viewspaces.component.html',
styleUrls: ['./viewspaces.component.css']
})
export class NewViewspacesComponent implements OnInit {
cards : Space[];
users : Observable<any[]>;
spaces : Observable<any[]>;
machines : Observable<any[]>;
constructor(private firestore: AngularFirestore) {
this.spaces = firestore.collection('spaces').valueChanges();
}
ngOnInit(): void {
const auth = getAuth();
const email = auth.currentUser?.email
this.cards = []
this.spaces.subscribe(res =>{
res.forEach(item =>{
if(item.email == email){
console.log(item.spaceID)
this.cards.push(item)
}
})
})
}
}
【问题讨论】:
-
查看此链接以获取答案,stackoverflow.com/questions/59640400/…
-
您需要在模板中设置按钮的点击事件和控制器中的触发功能。
标签: angular typescript google-cloud-firestore firebase-authentication