简体   繁体   English

内插的角度MatToolTip条件

[英]Angular MatToolTip Condition inside Interpolation

I am adding an angular condition for my MatToolTip. 我为MatToolTip添加了角度条件。 At first this following works for just 1 string assignment 首先,以下内容仅适用于1个字符串分配

matToolTip={{myData.name}}

But I need to add a condition like the following 但我需要添加如下条件

matToolTip={{ myData.hasName : myData.name, myData.hasNoName : myData.NoNameMessage }}

The data coming in has one or the other, never both. 传入的数据具有一个或另一个,而不会两者都存在。

I found another stackoverflow which ppl says the following works but not in my Angular 7 code 我发现了另一个stackoverflow,ppl表示以下工作有效,但在我的Angular 7代码中却无效

{{element.source == 1 ? 'upwork' : (element.source == 2 ? 'refer from friend' : '')}}

I tried putting them in single quote but no success. 我尝试将它们放在单引号中,但没有成功。 any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks. 谢谢。

尝试这个:

[matTooltip]="myData.hasName ? myData.name : (myData.hasNoName? 'myData.NoNameMessage' : null)"

You can also use interpolation to call a typescript function on component class that will perform complex if-else scenarios. 您还可以使用插值在组件类上调用一个打字稿函数,该脚本将执行复杂的if-else场景。 Something like this.. 像这样

<button mat-raised-button
    matTooltip={{getToolTip(source)}}
    >

Action 行动

getToolTip(source)  
{
var tooltip = '';
if(source==1)
{
  tooltip = 'upwork';
}
else
{
  if(source==2){
    tooltip = 'refer from friend';
  }
}
return tooltip;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM