获取 mat-card 组件的尺寸

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

Angular get size of mat-card component

问题

  1. 如何获取 `<mat-card>` 组件的坐标和大小?
  2. 也许这个问题很明显,但我在搜索了几天后还没有找到答案。也许访问 nativeElement 会有所帮助?
  3. 我有一个包含“添加”按钮和卡片列表的应用组件。
  4. export class AppComponent {
  5. id = 0
  6. public cards = new Array<Card>()
  7. newCardClick() {
  8. // todo 获取所有卡片的大小和坐标
  9. if (this.cards.length>0)
  10. console.log(this.cards[0].element.nativeElement)
  11. this.cards.push(new Card(
  12. this.id, "Test"+this.id, "", (new Date()).toString(), { x: 10, y: 10+this.id++*85}
  13. ))
  14. }
  15. }
  1. 卡片组件非常简单:
  2. export class CardComponent implements AfterViewInit{
  3. @Input() card: Card
  4. constructor(private element:ElementRef) {
  5. }
  6. ngAfterViewInit() {
  7. this.card.element = this.element
  8. }
  9. }
  10. export class Card{
  11. constructor(
  12. public id: number,
  13. public header: String,
  14. public description: String,
  15. public time:String,
  16. public position: Point) {
  17. }
  18. public element:ElementRef
  19. }

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

  1. <details>
  2. <summary>英文:</summary>
  3. How to get coordinates and size of `&lt;mat-card&gt;` component?
  4. Maybe question is obvious, but I haven&#39;t find answer for a couple of days searching web. Perhaps access to nativeElement will help?
  5. I have app component with &quot;add&quot; button and list of cards.
  6. ```angular
  7. export class AppComponent {
  8. id = 0
  9. public cards = new Array&lt;Card&gt;()
  10. newCardClick() {
  11. // todo get sizes and coordinates of all cards
  12. if (this.cards.length&gt;0)
  13. console.log(this.cards[0].element.nativeElement)
  14. this.cards.push(new Card(
  15. this.id, &quot;Test&quot;+this.id, &quot;&quot;, (new Date()).toString(), { x: 10, y: 10+this.id++*85}
  16. ))
  17. }
  18. }
  1. button {
  2. position: fixed;
  3. bottom:2em;
  4. right:2em;
  5. }
  1. &lt;div class=&quot;main__div&quot; #appElement&gt;
  2. &lt;app-card *ngFor=&quot;let card of cards&quot;
  3. [card]=&quot;card&quot;&gt;
  4. &lt;/app-card&gt;
  5. &lt;/div&gt;
  6. &lt;button mat-fab color=&quot;primary&quot;
  7. aria-label=&quot;New card&quot; (click)=&quot;newCardClick()&quot;&gt;
  8. &lt;mat-icon&gt;add&lt;/mat-icon&gt;
  9. &lt;/button&gt;

Card component is very simple:

  1. &lt;mat-card #cardElement class=&quot;card&quot;
  2. [cdkDragFreeDragPosition]=&quot;card.position&quot;
  3. cdkDrag&gt;
  4. &lt;mat-card-title&gt;card.header&lt;/mat-card-title&gt;
  5. &lt;mat-card-subtitle&gt;card.description&lt;/mat-card-subtitle&gt;
  6. &lt;/mat-card&gt;
  1. .mat-card {
  2. position: absolute;
  3. background-color: rgb(153, 237, 237);
  4. }
  1. export class CardComponent implements AfterViewInit{
  2. @Input() card: Card
  3. constructor(private element:ElementRef) {
  4. }
  5. ngAfterViewInit() {
  6. this.card.element = this.element
  7. }
  8. }
  9. export class Card{
  10. constructor(
  11. public id: number,
  12. public header: String,
  13. public description: String,
  14. public time:String,
  15. public position: Point) {
  16. }
  17. public element:ElementRef
  18. }

Here is link to repository with this code.

答案1

得分: 1

以下是翻译好的部分:

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

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

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

在您的 HTML 中使用 @ViewChild:

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

在组件类中使用它:

  1. @Component({
  2. selector: 'app-componentName',
  3. templateUrl: 'componentName.component.html',
  4. styleUrls: ['componentName.component.css']
  5. })
  6. export class ComponentName {
  7. @ViewChild('cardElement', { static: true }) cardElement: ElementRef;
  8. constructor(private renderer: Renderer2) {}
  9. ngAfterViewInit() {
  10. const cardRect = this.cardElement.nativeElement.getBoundingClientRect();
  11. const cardCoordinates = {
  12. top: cardRect.top,
  13. left: cardRect.left,
  14. width: cardRect.width,
  15. height: cardRect.height
  16. };
  17. console.log('Card coordinates:', cardCoordinates);
  18. }
  19. }

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

英文:

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

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

Here it go with your html
Use the @ViewChild

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

use this in component class

  1. @Component({
  2. selector: &#39;app-componentName&#39;,
  3. templateUrl: &#39;componentName.component.html&#39;,
  4. styleUrls: [&#39;componentName.component.css&#39;]
  5. })
  6. export class ComponentName {
  7. @ViewChild(&#39;cardElement&#39;, { static: true }) cardElement: ElementRef;
  8. constructor(private renderer: Renderer2) {}
  9. ngAfterViewInit() {
  10. const cardRect = this.cardElement.nativeElement.getBoundingClientRect();
  11. const cardCoordinates = {
  12. top: cardRect.top,
  13. left: cardRect.left,
  14. width: cardRect.width,
  15. height: cardRect.height
  16. };
  17. console.log(&#39;Card coordinates:&#39;, cardCoordinates);
  18. }
  19. }

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:

确定