禁用子组件中的MatRipple。

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

Disable MatRipple in Child Components

问题

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

  1. @ViewChild(MatRipple, { static: true }) ripple: MatRipple;
  2. launchRipple() {
  3. this.ripple.launch({
  4. persistent: false,
  5. centered: true,
  6. animation: { enterDuration: 1000, exitDuration: 1200 },
  7. color: 'rgba(33, 150, 243, 0.15)'
  8. });
  9. }

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

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

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

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

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

  1. <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"中这样做:

  1. @ViewChildren(MatRipple) ripple: MatRipple;
  2. 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:

  1. @ViewChild(MatRipple, { static: true }) ripple: MatRipple;
  2. launchRipple() {
  3. this.ripple.launch({
  4. persistent: false,
  5. centered: true,
  6. animation: { enterDuration: 1000, exitDuration: 1200 },
  7. color: &#39;rgba(33, 150, 243, 0.15)&#39;
  8. });
  9. }

On the text-message.component.html is:

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

The child component looks like this in the parent:

  1. &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:

  1. &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:

  1. @ViewChildren(MatRipple) ripple : MatRipple;
  2. 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指令的一个属性是

  1. @Input('matRippleDisabled')
  2. disabled: boolean

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

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

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

  1. @Input() disableRipple = false;

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

group-message.component.html保持不变

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

One of properties of MatRipple directive is

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

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

  1. &lt;div matRipple class=&quot;text-message&quot; [ngClass]=&quot;{&#39;unread&#39;: !message?.tmsRead}&quot; appDblClick
  2. (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

  1. @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

  1. &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
匿名

发表评论

匿名网友

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

确定