首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用视点角度聚焦在Mat卡上

无法使用视点角度聚焦在Mat卡上
EN

Stack Overflow用户
提问于 2019-06-25 18:16:18
回答 2查看 2.6K关注 0票数 4

我正试着从一张垫子卡的列表中把重点放在一张特定的卡片上。

但我总是犯错误

无法读取未定义的属性“焦点”

Stackblitz码

应该在点击按钮时添加一个金色的轮廓。

代码语言:javascript
运行
复制
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();

  }

}
EN

Stack Overflow用户

发布于 2019-06-25 18:53:44

  1. commentCard中的“@ViewChildren("commentCard")”应该引用某些内容,因此您需要放置一个组件/指令类型或一个模板变量。模板变量意味着在html-标记中添加#name,如下所示: <mat-card #commentCard *ngFor="let comment of comments" tabindex="0">Comment {{comment}}</mat-card>
  2. 您还需要告诉@ViewChildren,您想要得到DOM元素,而不是像这个@ViewChildren("commentCard", { read: ElementRef }) commentCardList: QueryList<ElementRef>;那样的角元素

https://stackblitz.com/edit/angular-gu5kpe-dqua65?file=app%2Fcard-overview-example.ts

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56759890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档