Angular 日期管道在文本框中无法正常工作

标签 angular typescript angular-material angular10

我的日期管道无法在 Angular 中运行。我只想显示为这种格式 MM/dd/yyyy'。如何解决?

typescript :

this.testDate = new Date(this.singleUser.createDate);
console.log(this.testDate); 

this.userForm = new FormGroup({
  userName: new FormControl(this.singleUser.userName),
  email: new FormControl(this.singleUser.email),
  createDate: new FormControl(this.singleUser.createDate)
});

Console Log: Wed Jul 22 2020 17:18:24 GMT-0700 (Pacific Daylight Time)

HTML:

<mat-form-field class="row" appearance="outline" floatLabel="always">
    <mat-label>Created Date</mat-label>
    <input matInput [readonly]="true" formControlName="createDate" [value] = "testDate | date: 'MM/dd/yyyy'" >
</mat-form-field>

enter image description here

日期未显示为 MM/dd/yyyy,并且具有额外的日期时间小时秒等

最佳答案

看,你不能同时使用 value 和 formControlName。 它还显示您的表单控件值,而不是按值属性显示值。

您需要为此添加一些解决方法。像这样

<mat-form-field class="row" appearance="outline" floatLabel="always">
    <mat-label>Created Date</mat-label>
    <input matInput [readonly]="true" (change)="funcFromTs($event.target.value)" [value] = "testDate | date: 'MM/dd/yyyy'" >
</mat-form-field>

Ts

funcFromTs(value: string) {
   this.userForm.get('createDate').patchValue(value);
}

关于 Angular 日期管道在文本框中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65414341/

相关文章:

html - Angular-cli 从外部 url 添加样式

angular - 在 Angular 中,我如何直接导航到延迟加载模块内的路径?

javascript - 如何处理 Angular 2 路由器测试版中的多个参数?

typescript :从父类获取扩展类的属性

javascript - 将 javascript map 合并到 Angular 中

angular - 如何按选定的值过滤数据?

javascript - 如何使用 token 在 Angular Material 表中设置服务器端分页

1000 行中的 Angular 6 MatTable 性能

Angular 2 Material 垫片尺寸

angular - 是否可以从 Angular 获取 AAD 访问 token ?