【发布时间】:2019-09-18 21:11:22
【问题描述】:
有人可以指导我使用 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>
但似乎不起作用。有没有人可以指导一下。
例外
TrainComponent.html:58 ERROR Error: InvalidPipeArgument: 'Unable to convert "apprTimestamp" into a date' for pipe 'DatePipe'
at invalidPipeArgumentError (common.js:5737)
at DatePipe.transform (common.js:6877)
at checkAndUpdatePureExpressionInline (core.js:34381)
at checkAndUpdateNodeInline (core.js:35155)
at checkAndUpdateNode (core.js:35090)
at debugCheckAndUpdateNode (core.js:36112)
at debugCheckDirectivesFn (core.js:36055)
at Object.eval [as updateDirectives] (TrainComponent.html:60)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:36043)
at checkAndUpdateView (core.js:35055)
谢谢!
【问题讨论】:
-
你把'apprTimestamp'作为字面量传递,当然不能转换成时间!
-
当我将其传递为
<input type="text" formControlName="{{ apprTimestamp | date:'medium' }}">时,我得到TrainComponent.html:58 ERROR Error: Cannot find control with unspecified name attribute at _throwError (forms.js:2749) at setUpControl (forms.js:2573) at FormGroupDirective.addControl (forms.js:6318) at FormControlName._setUpControl (forms.js:6969) -
你为什么要为表单控件name分配一个日期时间?这样做有什么目的?
-
apprTimestamp 的值类似于 2018-01-01 的字符串格式,我的错我应该提到它
-
这不是其他人的重点,您在代码中使用文字字符串
'apprTimestamp'而 not 是具有分配值的变量。那是因为你用单引号括住了你的变量名。
标签: javascript angular angular8 angular-pipe date-pipe