是否可以将当前日期和时间放在 mat-form-field 中?

Is it possible to put the current date and time inside a mat-form-field?

我有一个 mat-form-field,我希望当前时间显示在输入字段中。我能够将它添加到内部,但我在样式方面遇到了问题。 这是代码行:

<mat-label>Filing Time:</mat-label><br>
              <mat-form-field appearance="outline" class="width-1">
                <input
                  matInput
                  formControlName="filingTime"
                  readonly
                  required
                />
                {{ filingDate | date: 'HH:mm:ss' }}
              </mat-form-field>

你实际上并没有在你的输入“内部”添加它,它实际上被取代了,如果你想要实现的是自动填充输入,那么你需要使用 value 属性在 <input/> 上,但这当然会导致问题,因为您不能在 value 属性 中使用 date pipe,因此,您将不得不使用 ngModel这将改变您的方法,因为您使用的是 Reactive Forms 方法,但是您可以通过使用 standalone = true 用于您的 ngModel如果您不希望此字段出现在您的 form 中),或者您可以保留 name 属性 而不是 formControlName 也可以(如果你想让这个字段出现在你的 form 中,基本的实施将是这样的:

<mat-label>Filing Time:</mat-label><br>
<mat-form-field appearance="outline" class="width-1">
  <input matInput [ngModel]="filingDate | date: 'HH:MM:SS'" />
</mat-form-field>

我可以在查看模板其余部分和您的 component class.

中的更多代码后更新此答案以适合您的情况

希望对您有所帮助。