如果mat-form-field位于组件中,则表单布局会中断

问题描述 投票:0回答:1

如果我直接在表单中使用mat-form-fields,则字段显示在“列”流中,如下所示

<h1>Form 1</h1>
<form class="example-form">
  <mat-form-field >
  <input matInput  placeholder='Name' value='world' readonly='true'>
</mat-form-field>
 <mat-form-field >
  <input matInput  placeholder='Name' value='world' readonly='true'>
</mat-form-field>
 <mat-form-field >
  <input matInput  placeholder='Name' value='world' readonly='true'>
</mat-form-field>
 <mat-form-field >
  <input matInput  placeholder='Name' value='world' readonly='true'>
</mat-form-field>
</form>

但是,如果将mat-form-fields移动到角度组件,则字段将以行布局显示

<h1>Form 2</h1>
<form class="example-form1">
  <input-field></input-field>
  <input-field></input-field>
  <input-field></input-field>
  <input-field></input-field>
</form>

其中input-field组件的模板定义如下:

<mat-form-field >
  <input matInput  placeholder='Name' value="world" readonly='true'>
</mat-form-field>

这是最终结果。 Form1(无角度分量)和Form 2(字段角度分量)

Same form, with and without components

https://stackblitz.com/edit/angular-b3rxbf?embed=1&file=app/input-field.html

我应该对组件的CSS做些什么改变吗?

css angular-material angular2-template angular2-forms
1个回答
1
投票

你的第一个表单有max-width: 500px;,强制输入包装。第二种形式占用了所有可用空间,这恰好足以使所有字段都适合一行。

© www.soinside.com 2019 - 2024. All rights reserved.