获取 mat-card 组件的尺寸

huangapple go评论51阅读模式
英文:

Angular get size of mat-card component

问题

如何获取 `<mat-card>` 组件的坐标和大小?

也许这个问题很明显,但我在搜索了几天后还没有找到答案。也许访问 nativeElement 会有所帮助?

我有一个包含“添加”按钮和卡片列表的应用组件。

export class AppComponent {
  id = 0
  public cards = new Array<Card>()

  newCardClick() {
    // todo 获取所有卡片的大小和坐标
    if (this.cards.length>0)
      console.log(this.cards[0].element.nativeElement)
    this.cards.push(new Card(
      this.id, "Test"+this.id, "", (new Date()).toString(), { x: 10, y: 10+this.id++*85}
    ))
  }
}
卡片组件非常简单:

export class CardComponent implements AfterViewInit{
  @Input() card: Card

  constructor(private element:ElementRef) {
  }

  ngAfterViewInit() {
    this.card.element = this.element
  }
}

export class Card{
    constructor(
      public id: number,
      public header: String,
      public description: String,
      public time:String, 
      public position: Point) {
      }
      public element:ElementRef
    }

以上是您提供的代码的翻译部分。如果您需要任何进一步的帮助,请随时告诉我。


<details>
<summary>英文:</summary>

How to get coordinates and size of `&lt;mat-card&gt;` component?

Maybe question is obvious, but I haven&#39;t find answer for a couple of days searching web. Perhaps access to nativeElement will help?

I have app component with &quot;add&quot; button and list of cards.
```angular
export class AppComponent {
  id = 0
  public cards = new Array&lt;Card&gt;()

  newCardClick() {
    // todo get sizes and coordinates of all cards
    if (this.cards.length&gt;0)
      console.log(this.cards[0].element.nativeElement)
    this.cards.push(new Card(
      this.id, &quot;Test&quot;+this.id, &quot;&quot;, (new Date()).toString(), { x: 10, y: 10+this.id++*85}
    ))
  }
}
button {
    position: fixed;
    bottom:2em;
    right:2em;
}
&lt;div class=&quot;main__div&quot; #appElement&gt;
  &lt;app-card *ngFor=&quot;let card of cards&quot; 
    [card]=&quot;card&quot;&gt;
  &lt;/app-card&gt;
&lt;/div&gt;
&lt;button mat-fab color=&quot;primary&quot; 
    aria-label=&quot;New card&quot; (click)=&quot;newCardClick()&quot;&gt;
    &lt;mat-icon&gt;add&lt;/mat-icon&gt;
&lt;/button&gt;

Card component is very simple:

&lt;mat-card #cardElement class=&quot;card&quot; 
[cdkDragFreeDragPosition]=&quot;card.position&quot;
cdkDrag&gt;
    &lt;mat-card-title&gt;card.header&lt;/mat-card-title&gt;
    &lt;mat-card-subtitle&gt;card.description&lt;/mat-card-subtitle&gt;
&lt;/mat-card&gt;
.mat-card {
    position: absolute;
    background-color: rgb(153, 237, 237);
}
export class CardComponent implements AfterViewInit{
  @Input() card: Card

  constructor(private element:ElementRef) {
  }

  ngAfterViewInit() {
    this.card.element = this.element
  }
}

export class Card{
    constructor(
      public id: number,
      public header: String,
      public description: String,
      public time:String, 
      public position: Point) {
      }
      public element:ElementRef
    }

Here is link to repository with this code.

答案1

得分: 1

以下是翻译好的部分:

要获取 Angular 中 <mat-card> 组件的坐标和大小,您可以使用 Angular 提供的 ElementRef 和 Renderer2。

需要在您的组件文件中导入依赖项:

import { Component, ElementRef, Renderer2, ViewChild } from '@angular/core';

在您的 HTML 中使用 @ViewChild:

<mat-card #cardElement>...</mat-card>

在组件类中使用它:

@Component({
  selector: 'app-componentName',
  templateUrl: 'componentName.component.html',
  styleUrls: ['componentName.component.css']
})
export class ComponentName {
  @ViewChild('cardElement', { static: true }) cardElement: ElementRef;

  constructor(private renderer: Renderer2) {}

  ngAfterViewInit() {
    const cardRect = this.cardElement.nativeElement.getBoundingClientRect();
    const cardCoordinates = {
      top: cardRect.top,
      left: cardRect.left,
      width: cardRect.width,
      height: cardRect.height
    };

    console.log('Card coordinates:', cardCoordinates);
  }
}

希望这能帮助您获取坐标。

英文:

Not sure if you are looking for whole code or what,let me give you an example how you can achieve this

To get the coordinates and size of a <mat-card> component in Angular, you can use the ElementRef and Renderer2 provided by Angular
need to import dependencies in your component file

import { Component, ElementRef, Renderer2, ViewChild } from &#39;@angular/core&#39;;

Here it go with your html
Use the @ViewChild

&lt;mat-card #cardElement&gt;...&lt;/mat-card&gt;

use this in component class

@Component({
  selector: &#39;app-componentName&#39;,
  templateUrl: &#39;componentName.component.html&#39;,
  styleUrls: [&#39;componentName.component.css&#39;]
})
export class ComponentName {
  @ViewChild(&#39;cardElement&#39;, { static: true }) cardElement: ElementRef;

  constructor(private renderer: Renderer2) {}

  ngAfterViewInit() {
    const cardRect = this.cardElement.nativeElement.getBoundingClientRect();
    const cardCoordinates = {
      top: cardRect.top,
      left: cardRect.left,
      width: cardRect.width,
      height: cardRect.height
    };

    console.log(&#39;Card coordinates:&#39;, cardCoordinates);
  }
}

Hope this will help you to get Coordinates.

huangapple
  • 本文由 发表于 2023年6月8日 17:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定