角度材质5 - 在垫子列表中显示全文(自动换行)

时间:2017-11-19 13:01:23

标签: angular angular-material

我正在创建一个聊天框,每条消息都是一个角度材料垫列表。 https://material.angular.io/components/list/overview

但是,如果消息真的很长而不是增加高度并进入下一行,它会剪切消息并显示一个椭圆。如下图所示。

enter image description here

 <mat-list dense>     
    <mat-list-item class="chat-message-body" *ngIf="auth._id !== message.author._id" fxLayoutAlign="" dir="ltl">
        <div matLine>
            <b>{{message.author.profile.username}} </b>
            <span>{{message.created_at | date:'shortTime'}} </span>
        </div>
        <span matLine> {{message.body}} </span>
        <img matListAvatar class="img-box" src="http://via.placeholder.com/30x30" alt="...">
    </mat-list-item>
</mat-list>

如何强制它显示全文

5 个答案:

答案 0 :(得分:16)

使用以下css:

::ng-deep .mat-list .mat-list-item .mat-line{
     word-wrap: break-word;
    white-space: pre-wrap;
}

::ng-deep .mat-line{
  word-wrap: break-word !important;
  white-space: pre-wrap !important;
}

mat-list-item的高度限制为48px所以我们需要在大文本的情况下覆盖

::ng-deep  .mat-list .mat-list-item{
  height:initial!important;
}

演示:https://plnkr.co/edit/tTlhYgTkSz1QcgqjCfqh?p=preview

Link了解有关自动换行和white-spac

的更多信息

答案 1 :(得分:1)

我创建了这个css / scss类。

    /* CSS */
    .mat-list-item.mat-list-item-word-wrap {
      height: initial !important;
    }
    .mat-list-item-word-wrap .mat-line {
      word-wrap: break-word !important; 
      white-space: pre-wrap !important;
    }

    /* SCSS */
    .mat-list-item.mat-list-item-word-wrap {
      height: initial !important;
      .mat-line {
        word-wrap: break-word !important; 
        white-space: pre-wrap !important;
      }
    }

HTML:

    <mat-list>
      <mat-list-item class="mat-list-item-word-wrap">
        <mat-icon mat-list-icon>description</mat-icon>
        <h3 mat-line>{{ description }}</h3>
      </mat-list-item>
    </mat-list>

请参阅此处的示例:https://stackblitz.com/edit/angular-6mhz2x

答案 2 :(得分:0)

万一阅读这篇文章的人希望包裹密集的席位列表<mat-list dense>,请在上面的mohit uprim's答案中添加[dense]

::ng-deep .mat-list[dense] .mat-list-item .mat-line {
  word-wrap: break-word;
  white-space: pre-wrap;
}

答案 3 :(得分:0)

只需用<mat-list-item>标签将整个段落包装在<p> ,这将强制使用正确的样式。很有道理...在这种情况下不需要样式。

<mat-list-item>
  <p>
    My super long text..........
  </p>
</mat-list-item>

答案 4 :(得分:-1)

您是否尝试使用CSS属性word-breakoverflow-break