我正试着从一张垫子卡的列表中把重点放在一张特定的卡片上。
但我总是犯错误
无法读取未定义的属性“焦点”
应该在点击按钮时添加一个金色的轮廓。
import { Component, OnInit, QueryList, ElementRef, ViewChildren } from '@angular/core';
/**
* @title Basic cards
*/
@Component({
selector: 'card-overview-example',
templateUrl: 'card-overview-example.html',
styleUrls: ['card-overview-example.css'],
})
export class CardOverviewExample implements OnInit {
comments: number[];
@ViewChildren("commentCard") commentCardList: QueryList<ElementRef>;
ngOnInit() {
this.comments = [1, 2, 3, 4, 5, 6, 7, 8];
}
focus() {
const elementRef = this.commentCardList.find((item, index) => index === 4);
elementRef.nativeElement.focus();
//this.commentCardList[4].nativeElement.focus();
}
}发布于 2019-06-25 18:53:44
@ViewChildren("commentCard")”应该引用某些内容,因此您需要放置一个组件/指令类型或一个模板变量。模板变量意味着在html-标记中添加#name,如下所示:
<mat-card #commentCard *ngFor="let comment of comments" tabindex="0">Comment {{comment}}</mat-card>@ViewChildren,您想要得到DOM元素,而不是像这个@ViewChildren("commentCard", { read: ElementRef }) commentCardList: QueryList<ElementRef>;那样的角元素https://stackblitz.com/edit/angular-gu5kpe-dqua65?file=app%2Fcard-overview-example.ts
https://stackoverflow.com/questions/56759890
复制相似问题