Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度formControlName中的日期管道_Javascript_Angular_Angular8_Angular Pipe_Date Pipe - Fatal编程技术网

Javascript 角度formControlName中的日期管道

Javascript 角度formControlName中的日期管道,javascript,angular,angular8,angular-pipe,date-pipe,Javascript,Angular,Angular8,Angular Pipe,Date Pipe,有人能指导我用formControlName实现angular 8中的日期管道吗 train.component.html <mat-grid-tile colspan=1 rowspan=1> <div class="train-control"> <input type="text" formControlName="{{ 'apprTimestamp' | date:'medium' }}"> </div> &

有人能指导我用formControlName实现angular 8中的日期管道吗

train.component.html

<mat-grid-tile colspan=1 rowspan=1>
    <div class="train-control">
        <input type="text" formControlName="{{ 'apprTimestamp' | date:'medium' }}">
    </div>
</mat-grid-tile>
谢谢

如果确实要将日期值分配给formControlName,则排除包含时间戳值的变量的“”

<input type="text" name="someName" formControlName="{{ apprTimestamp | date:'medium' }}">

您也可以在组件中使用日期管道,并从中修改日期。这只会在预设日期下工作一次。但你的问题中也没有包括任何其他内容,所以我们可以完全忽略这一点

因此,我建议如下:

import { DatePipe } from '@angular/common';

@Component({
  selector: '...',
  templateUrl: '...',
  styleUrls: ['...'],
  providers: [DatePipe]
})
将其注入构造函数,然后使用::

constructor(private fb: FormBuilder, private datePipe: DatePipe) {
  this.myForm = this.fb.group({
    field1: [this.datePipe.transform(new Date(), 'medium')]
  })
}
然后从模板中删除管道


您将“apptimestamp”作为文本字符串传递,当然它不能转换为时间!当我将其作为
传递时,我得到
TrainComponent.html:58错误:在FormControlName的setUpControl(forms.js:2573)的setUpControl(forms.js:2749)的FormGroupDirective.addControl(forms.js:6318)的setUpControl(forms.js:6969)中找不到具有未指定名称属性的控件
为什么要尝试为表单控件名称分配日期时间?它的作用是什么?apprTimestamp是字符串格式的值,类似2018-01-01,我不好,我应该提到它,这不是其他人都在说的重点,您在代码中使用的是文本字符串
“apprTimestamp”
,而不是带有赋值的变量。这是因为您用单引号括住了变量名。不起作用,失败的原因是异常
TrainComponent.html:58错误:在FormGroupDirective.addControl(forms.js:6318)的setUpControl(forms.js:2573)的_throwError(forms.js:2749)中找不到具有未指定名称属性的控件位于FormControlName.\u位于FormControlName.ngOnChanges(forms.js:6892)的setUpControl(forms.js:6969)
constructor(private fb: FormBuilder, private datePipe: DatePipe) {
  this.myForm = this.fb.group({
    field1: [this.datePipe.transform(new Date(), 'medium')]
  })
}