【问题标题】:The <mat-form-field> Tag restricts the width of the form and doesn't match the width of the container<mat-form-field> 标签限制了表单的宽度,与容器的宽度不匹配
【发布时间】:2019-05-28 17:50:31
【问题描述】:

我正在使用 Angular 编辑表单,但效果不佳。我在使用Chrome开发工具调试的时候发现mat-form-field限制了输入区域的宽度,而我没有放任何CSS代码来指定宽度。

form和p标签的宽度:460,mat-form-field的宽度:180。除了容器部分,我没有给代码添加任何样式。

<div class="container"
fxLayout="row"
fxLayout.sm="column"
fxLayout.xs="column"
fxLayoutAlign.gt-md="space-around center"
fxLayoutGap="20px" 
fxLayoutGap.xs="0">

<div fxFlex *ngIf="dish">
...
</div>

<div fxFlex *ngIf="dish" class="full-width">
    <h3>
      <b>Comments</b>
    </h3>
    <mat-list *ngIf="dish.comments">
        <mat-list-item *ngFor="let comment of dish.comments">
          <p matline>
          <span>{{comment.comment}}<br></span>
          <span>{{comment.rating}} Stars<br></span>
          <span>-- {{comment.author}} {{comment.date | date}}</span>
          </p>
      </mat-list-item>
    </mat-list>

    <form novalidate #fform="ngForm" [formGroup]="commentForm" (ngSubmit)="onSubmit()">
    <p>
      <mat-form-field>
        <input matInput formControlName="name" placeholder="name" type="text" required>
        <mat-error *ngIf="formErrors.name">{{formErrors.name}}</mat-error>
      </mat-form-field>
    </p>
    <p>
      <mat-slider thumbLabel tickInterval="1" min="0" max="5" value="5"></mat-slider>
    </p>
    <p>
      <mat-form-field>
        <textarea matInput formControlName="comment" placeholder="comment" rows=8 required></textarea>
        <mat-error *ngIf="formErrors.comment">{{formErrors.comment}}</mat-error>
      </mat-form-field>
    </p>
      <button type="submit" mat-button class="background-primary text-floral-white" [disabled]="!commentForm.valid">Submit</button>
  </form>
</div>

<div [hidden]="dish">
  <mat-spinner></mat-spinner><h4>Loading . . . Please Wait</h4>
</div>

我想将 mat-form-field 宽度与表单宽度相匹配,从 180 到 460。我该怎么做?

【问题讨论】:

    标签: angular html angular-material


    【解决方案1】:

    width: 100% 应用于 mat-form-field 元素;这将导致它匹配其父级的宽度。您可能需要稍微调整一下包含元素的样式,以获得您想要的确切外观。

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 1970-01-01
      • 2020-10-29
      • 2013-02-21
      • 2018-09-27
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      相关资源
      最近更新 更多