禁用子组件中的MatRipple。

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

Disable MatRipple in Child Components

问题

我有一个名为"text-message"的子组件,它与多个父组件共享。它使用了一个"matRipple"效果。我需要在除了一个名为"group-message"的父组件之外的每个父组件上都使用这个效果。在"text-message.component.ts"文件中存在以下代码:

@ViewChild(MatRipple, { static: true }) ripple: MatRipple;

launchRipple() {
  this.ripple.launch({
    persistent: false,
    centered: true,
    animation: { enterDuration: 1000, exitDuration: 1200 },
    color: 'rgba(33, 150, 243, 0.15)'
  });
}

在"text-message.component.html"中是这样的:

<div matRipple class="text-message" [ngClass]="{'unread': !message?.tmsRead}" appDblClick
  (dblclick)="onClick($event)">

在父组件中,子组件的使用方式如下:

<text-message [message]="message" [timeOffset]="currentSiteTimeOffset$ | async" [showStatus]="false"></text-message>

我需要在"group-message.component.html"中关闭这个效果。这可能吗?我尝试了这样的方式:

<text-message [message]="message" [timeOffset]="currentSiteTimeOffset$ | async" [showStatus]="false" [disableRipple]="true"></text-message>

但是我收到了一个错误信息:"can't bind to 'disableRipple' since it isn't a known property of 'text-message'"。

我还尝试了在"group-message.component.ts"中这样做:

@ViewChildren(MatRipple) ripple: MatRipple;
this.ripple.disabled;

我对Angular非常陌生,仍在学习如何处理父组件、子组件和兄弟组件。非常感谢您的帮助。

英文:

I have a child component called "text-message" that is shared with several parent components. It uses a "matRipple" effect. I need this effect on every parent except for one named "group-message". In the text-message.component.ts file the following code exists:

@ViewChild(MatRipple, { static: true }) ripple: MatRipple;

  launchRipple() {
    this.ripple.launch({
      persistent: false,
      centered: true,
      animation: { enterDuration: 1000, exitDuration: 1200 },
      color: &#39;rgba(33, 150, 243, 0.15)&#39;
    });
  }

On the text-message.component.html is:

&lt;div matRipple class=&quot;text-message&quot; [ngClass]=&quot;{&#39;unread&#39;: !message?.tmsRead}&quot; appDblClick
  (dblclick)=&quot;onClick($event)&quot;&gt;

The child component looks like this in the parent:

&lt;text-message [message]=&quot;message&quot; [timeOffset]=&quot;currentSiteTimeOffset$ | async&quot; [showStatus]=&quot;false&quot;&gt;&lt;/text-message&gt;

I need to turn this off if it is in the "group-message.component.html". Is this possible? I tried this:

&lt;text-message [message]=&quot;message&quot; [timeOffset]=&quot;currentSiteTimeOffset$ | async&quot; [showStatus]=&quot;false&quot; [disableRipple]=&quot;true&quot; &gt;&lt;/text-message&gt;

But I receive an error "can't bind to 'disableRipple"' since it isn't a known property of 'text-message'".

I also tried this in the group-message.component.ts:

  @ViewChildren(MatRipple) ripple : MatRipple;
 this.ripple.disabled;

I am very new to Angular and am still learning how to work with parents, children, and siblings. Any assistance is greatly appreciated.

答案1

得分: 0

MatRipple指令的一个属性是

@Input('matRippleDisabled')
disabled: boolean

我们可以通过在text-message.component.html中添加一个输入来使用它

<div matRipple class="text-message" [ngClass]="{'unread': !message?.tmsRead}" appDblClick
  (dblclick)="onClick($event)" [matRippleDisabled]="disableRipple">

当然,在text-message.component.ts中需要声明disableRipple变量。我们将其声明为@Input,因为我们希望在新值到达时替换默认值

@Input() disableRipple = false;

下一步是通过从父组件传递true来设置disableRipple变量的值。

group-message.component.html保持不变

<text-message [message]="message" [timeOffset]="currentSiteTimeOffset$ | async" [showStatus]="false" [disableRipple]="true" ></text-message>
英文:

One of properties of MatRipple directive is

@Input(&#39;matRippleDisabled&#39;)
disabled: boolean

We can make a use of it by adding an input to the text-message.component.html

&lt;div matRipple class=&quot;text-message&quot; [ngClass]=&quot;{&#39;unread&#39;: !message?.tmsRead}&quot; appDblClick
  (dblclick)=&quot;onClick($event)&quot; [matRippleDisabled]=&quot;disableRipple&quot;&gt;

Of course the declaration of disableRipple variable is needed in text-message.component.ts. We declare it as @Input, since we want to replace the default value as soon as the new one arrives

@Input() disableRipple = false;

The next step would be setting a value of disableRipple variable by passing true from parent component.

The group-message.component.html would remain like this

&lt;text-message [message]=&quot;message&quot; [timeOffset]=&quot;currentSiteTimeOffset$ | async&quot; [showStatus]=&quot;false&quot; [disableRipple]=&quot;true&quot; &gt;&lt;/text-message&gt;

huangapple
  • 本文由 发表于 2023年8月9日 01:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861957.html