将角形材质的文本区域设置为组件的50%

时间:2018-12-04 18:30:30

标签: css angular angular-material2

我正在用角度6创建一个应用程序(我计划将其迁移到ng7),并且其中一个组件需要添加2个元素,即ng材质的textarea和我的自定义组件。每个都应占可用高度的50%。我在textarea上遇到问题,因为生成的mat-form-field-wrapper仅占用所需的空间,而我无法在该元素中实现目标(我不想使用任何:: ng-deep解决方案,因为不推荐)

这是我到目前为止所做的(当然是示例):

<form [formGroup]="myGroup" fxLayout="column" fxLayoutAlign="center stretch" fxFlex="1 1 100%">
  <mat-form-field fxFlex="1 1 100%" fxLayout="column" fxLayoutAlign="start stretch">
    <textarea matInput formControlName="myControl" fxFlex="1 1 100%"></textarea>
  </mat-form-field>
</form>

<my-custom-component *ngIf="somevariable" [variable]="somevariable"></my-custom-component>

1 个答案:

答案 0 :(得分:1)

您需要在全局样式表中覆盖样式,以替代使用deep的方法。由于表单字段元素的布局方式和可变的填充等原因,我不确定您能否在所有条件下都能完美地完成此工作,但这是一个起点:

<form [formGroup]="myGroup" style="display: flex; flex-direction: column; height: 50%;">
  <mat-form-field class="stretch-height">
    <textarea matInput formControlName="myControl"></textarea>
  </mat-form-field>
</form>

<my-custom-component style="height: 50%" *ngIf="somevariable" [variable]="somevariable"></my-custom-component>

全局CSS:

.mat-form-field.stretch-height,
.mat-form-field.stretch-height .mat-form-field-flex,
.mat-form-field.stretch-height textarea {
  height: 100%;
}
.mat-form-field.stretch-height .mat-form-field-wrapper {
  height: calc(100% - 1.34375em);
}
.mat-form-field.stretch-height .mat-form-field-infix {
  height: calc(100% - 1.34375em - 2px);
}

至关重要的是,父容器的高度必须为100%或您希望的高度。

这是StackBlitz上的演示。