【问题标题】:Angular Material mat-form-field Placeholder text on multiple linesAngular Material mat-form-field 多行占位符文本
【发布时间】:2018-10-20 01:16:20
【问题描述】:

我使用一个普通的mat-form-field,里面有一个textarea。我的问题是这个 Textarea 的占位符文本相当长。在空间更有限的移动设备中,此占位符文本会被 Angular Material 截断并带有省略号。

我希望通过向下移动到下一行来调整占位符文本以适应空间限制。因此,例如,而不是:

This is a really long text and it cannot fit on a single li...

我想要:

This is a really long text and it cannot fit on a single
line

我已经尝试了各种方法来更改mat-input-placeholdermat-input-placeholder-wrapper 的CSS,但没有成功。我知道解决方案的一部分涉及更改 text-overflow 属性(如下),但我无法获得其他部分。

::ng-deep .mat-input-placeholder {
  text-overflow: clip;
}

谁能帮忙?

谢谢!

【问题讨论】:

    标签: angular angular-material angular-material-5


    【解决方案1】:

    换行:

    <textarea></textarea>
    
    <textarea
      matInput
      #fileName="ngModel"
      name="fileName"
      [(ngModel)]="action!.params.file!.originalName"
      type="text"
      autocomplete="off"
      autocapitalize="off"
      readonly
      required
      [matTooltip]="action!.params.file!.originalName"
    ></textarea>
    

    溢出剪辑文本:

    <input/>
    
    <input
      matInput
      #fileName="ngModel"
      name="fileName"
      [(ngModel)]="action!.params.file!.originalName"
      type="text"
      autocomplete="off"
      autocapitalize="off"
      readonly
      required
      [matTooltip]="action!.params.file!.originalName"
    />
    

    【讨论】:

      【解决方案2】:

      ::ng-deep 已弃用。

      相反,您应该使用ViewEncapsulation 并控制组件内部的 CSS。

      @Component({
        selector: 'app-example',
        templateUrl: './example.component.html',
        styleUrls: ['./example.component.css'],
        encapsulation: ViewEncapsulation.None,
      })
      

      对于占位符(从元素中删除 placeholder):

      &lt;mat-placeholder style="white-space: normal;"&gt;{{question.label}}&lt;/mat-placeholder&gt;

      然后在example.component.css 中添加不带::ng-deep 的CSS 样式

      .mat-form-field-label {
        white-space: normal;
      }
      
      textarea.mat-input-element {
        padding: 0px 0;
        margin: 5px 0;
      }
      
      .mat-input-placeholder {
        white-space: normal;
      }
      

      【讨论】:

      • 虽然不推荐使用 ng-deep,但没有指定删除时间:angular.io/guide/deprecations#index
      • 此外,建议的 ViewEncapsulation.None 技术比 ng-deep 更困难,因为您现在将在整个站点中泄露您的样式。
      猜你喜欢
      • 2020-05-03
      • 2019-12-14
      • 2019-08-16
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2019-09-25
      • 2018-07-10
      相关资源
      最近更新 更多