【问题标题】:How to show full text and wrap in menu item in angular material如何在角度材料中显示全文并在菜单项中换行
【发布时间】:2019-05-30 01:26:47
【问题描述】:

我正在尝试制作一个通知系统,所以我正在使用来自 angular material 的mat-menu。我增加了menu-item 的宽度,但问题是它没有显示完整的内容。它只显示一行中的文本。

但如果文本超出该行,我想将文本换行,例如此处

我的compontent.html

  <button mat-button [matMenuTriggerFor]="notification"><mat-icon>add_alert</mat-icon></button>
  <mat-menu #notification="matMenu" [overlapTrigger]="false" class="my-full-width-menu">
    <button mat-menu-item style="white-space: normal">
      Learn one way to build applications with Angular and reuse your code and abilities to build
      apps for any deployment target. For web, mobile web, native mobile and native desktop.
    </button>
    <button mat-menu-item>Item 2</button>
  </mat-menu>

CSS

.mat-menu-panel.my-full-width-menu {
  max-width: none;
  width: 100vw;
  margin-left: -8px;
  margin-top: 24px;
}

请告诉我该怎么做。

【问题讨论】:

  • 是 css word-break: break-word; 你是什么意思?
  • @RamondeVries,我试过了,但没用。

标签: javascript html css angular angular-material


【解决方案1】:

mat-menu-item 类具有静态 line-height 和静态 height48px,这可以防止其自动换行。

::ng-deep button.mat-menu-item {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    cursor: pointer;
    outline: 0;
    border: none;
    -webkit-tap-highlight-color: transparent;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    /** set line height to desired height */
    line-height: 18px;
    /** set height to auto */
    height: auto;
    padding: 0 16px;
    text-align: left;
    text-decoration: none;
    max-width: 100%;
    position: relative;
}

要设置菜单宽度,请使用以下内容。

::ng-deep div.mat-menu-panel {
    min-width: 112px;
      /** set width to 500px */
    max-width: 500px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(100vh - 48px);
    border-radius: 4px;
    outline: 0;
}

堆栈闪电战

https://stackblitz.com/edit/angular-qzbutg?embed=1&file=app/menu-overview-example.css

【讨论】:

  • 它不显示全文,它只是截断看不见的文本。
  • 查看添加的 stackblitz 示例来回答。
  • 当我从 .ts 中删除 encapsulation: ViewEncapsulation.None, 时它可以工作,但是如果我删除它,那么宽度将设置为默认大小 280px,然后如何增加宽度大小。
  • 查看修改后的答案和 stackblitz 的菜单宽度到500px
  • ::ng-deep 已弃用,请勿使用此解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 2015-12-21
  • 2020-04-26
相关资源
最近更新 更多