简体   繁体   English

angular 2 材质 matTooltip 多线

[英]angular 2 material matTooltip multiline

I'm new to Angular 2 Material.我是 Angular 2 Material 的新手。 What's the preferred way to make a tool tip multi-line?使工具提示多行的首选方法是什么?

For example, I might have the following tool tip:例如,我可能有以下工具提示:

AA BBBBBBBBBB CCCC DDDDD

And, I may want to to have it display in a multi-line format like this:而且,我可能想让它以这样的多行格式显示:

AA BBBBBBBBBB 
CCCC 
DDDDD

First, create a class for your tooltip, to make it break lines on line breaks.首先,为您的工具提示创建一个类,使其在换行符处换行。

.my-tooltip {
    white-space: pre-line;
}

Then, add the class to your tooltip and insert line breaks with the Unicode code 
然后,将该类添加到您的工具提示中并插入带有 Unicode 代码
换行符

<span 
    matTooltip="First line&#13;Second line"
    matTooltipClass="my-tooltip">
</span>

you can achieve the same thing without even the need of \\n or你甚至可以在不需要 \\n 或
just write your content as it is for example :只需按原样编写您的内容,例如:

" abc
  abc
  abc " 

if you need it to be multilined如果你需要它是多行的

All you need to do is你需要做的就是

::ng-deep .mat-tooltip  {
    white-space: pre-line    !important;
}

add the above to your css将上述内容添加到您的 css 中

UPDATE:: 2021更新:: 2021

The above solution is pretty old and ::ng-deep is depricated as pointed out by @bsplosion in the comments.如@bsplosion 在评论中指出的那样,上述解决方案已经很老了,::ng-deep 已被弃用。 You can write the above css in your global stylesheet.您可以在全局样式表中编写上述 css。 depending where you placed your global.取决于您放置全局的位置。

.mat-tooltip  {
    white-space: pre-line;
}

If you don't want to use the above way like previous solution of rodris you can just add the class directly.如果您不想像之前的rodris解决方案那样使用上述方式,则可以直接添加该类。

<span 
    matTooltip="First line&#13;Second line"
    [matTooltipClass]="'my-tooltip'">
</span>

In My Example I am using angular 6, Material Design.在我的示例中,我使用的是 angular 6,Material Design。 My Issue was 'I have array of string' and I have to show it in multiline MatTooltip.我的问题是“我有字符串数组”,我必须在多行 MatTooltip 中显示它。 I handle the situation and it works for me.我处理这种情况,它对我有用。 Here is code这是代码

 .mat-tooltip { white-space: pre-line; }
 <p matTooltip="{{myArray.join('\\n')}}" >...</p>

none of this worked for me in my project ie using angular 7,8 I found the solution from在我的项目中,这些都不适合我,即使用 angular 7,8 我找到了解决方案
here DEMO这里演示

In html在 html 中

<img src="assets/icons/about.png"  width="15px" height="15px" matTooltip="{{getMoreInformation()}}"  matTooltipClass="test"/>   

In ts file在 ts 文件中

 getMoreInformation(): string {
    return 'Address : Home \n  Tel : Number';// \n represent break line here
    }

in your style.css file(remember project style.css)在你的 style.css 文件中(记住项目 style.css)

.test {
    white-space: pre-line;
  }

Hope it help someone.希望它可以帮助某人。 ps keep coding ps继续编码

You can simply do:你可以简单地做:

HTML : HTML :

<button matTooltip="AA BBBBBBBBBB 
   CCCC 
   DDDDD">
   Hello world !
</button>

CSS : CSS :

::ng-deep .mat-tooltip {
  white-space: pre-line; // Allow line break
}

Result :结果

工具提示

You can set the tooltip style using ng-deep: 您可以使用ng-deep设置工具提示样式:

::ng-deep .mat-tooltip  {
    max-width: 60px !important; // for example
}

DEMO DEMO

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

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