[英]Error in Angular: Template parse errors: Can't bind to 'datetime' since it isn't a known property of 'time'
I want to add attribute datetime
to time
element in Angular: 我想在Angular中将属性
datetime
添加到time
元素:
<time class="updated" [datetime]="post.modified_gmt"> {{ post.modified_gmt | date: 'short'}} </time>
But get next error: 但得到下一个错误:
Error: Template parse errors: Can't bind to 'datetime' since it isn't a known property of 'time'.
错误:模板解析错误:无法绑定到'datetime',因为它不是'time'的已知属性。 (" class="entry-date published">{{ post.date_gmt | date: 'short'}}
(“class =”entry-date published“> {{post.date_gmt | date:'short'}}
What the solution? 解决方案是什么? Thanks for any help
谢谢你的帮助
correct the lowercase [dateTime]
. 更正小写
[dateTime]
。
<time
class="updated"
[dateTime]="post.modified_gmt">
{{ post.modified_gmt | date: 'short'}}
</time>
Have try. 试试吧。
You are using template binding syntax on a non-component element. 您正在非组件元素上使用模板绑定语法 。
When you write [datetime]="..."
it tells Angular to bind the expression to an @Input()
named datetime
, but the <time>
tag is not part of any component or directive. 当您编写
[datetime]="..."
它会告诉Angular将表达式绑定到名为datetime
的@Input()
,但<time>
标记不是任何组件或指令的一部分。
You have to use attribute syntax if you want Angular to assign the value to the element's attributes. 如果希望Angular将值分配给元素的属性,则必须使用属性语法。
<time class="updated" [attr.datetime]="post.modified_gmt">{{ post.modified_gmt | date: 'short'}}</time>
Normally, you can assign expressions to attributes using {{ }}
syntax. 通常,您可以使用
{{ }}
语法将表达式分配给属性。 Such as <time datetime="{{post.modified_gmt}}"></time>
, but this only works if Angular has the attribute in it's whitelist of supported HTML elements and attributes. 例如
<time datetime="{{post.modified_gmt}}"></time>
,但这只适用于Angular在其受支持的HTML元素和属性的白名单中具有该属性的情况。 It appears datetime
is not supported. 它似乎不支持
datetime
。
You can suppress the error message by adding NO_ERRORS_SCHEMA
to your @NgModule()
but this only hides the error. 您可以通过将
NO_ERRORS_SCHEMA
添加到@NgModule()
来抑制错误消息,但这只会隐藏错误。 The expression will still not set the datetime
attribute. 表达式仍然不会设置
datetime
属性。
There is a way to add your own custom schemas using CUSTOM_ELEMENTS_SCHEMA
and tell Angular to support the datetime
attribute, but I don't know how it works and don't think it's worth it. 有一种方法可以使用
CUSTOM_ELEMENTS_SCHEMA
添加自己的自定义模式,并告诉Angular支持datetime
属性,但我不知道它是如何工作的,并且不认为它是值得的。 Just use [attr.datetime]
instead. 只需使用
[attr.datetime]
。
Declare datetime
as @Input() datetime;
将
datetime
声明为@Input() datetime;
inside component for time as below : 内部组件的时间如下:
import { Component, Input } from '@angular/core';
@Component({
selector: 'time',
template: `your template goes here`
})
export class TimeComponent {
@Input() datetime: any;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.