简体   繁体   English

Angular中的错误:模板解析错误:无法绑定到'datetime',因为它不是'time'的已知属性

[英]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.

相关问题 错误:模板解析错误:无法绑定到“ ngForOf”,因为它不是“模板”的已知属性 - Error: Template parse errors: Can't bind to 'ngForOf' since it isn't a known property of 'template' Angular 6 日历模板解析错误:无法绑定到“视图”,因为它不是“div”的已知属性 - Angular 6 Calendar Template parse errors: Can't bind to 'view' since it isn't a known property of 'div' Angular 2:模板解析错误:无法绑定到&#39;ngModel&#39;,因为它不是&#39;input&#39;的已知属性 - Angular 2: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input' Angular 2 Beta路由错误:模板解析错误:无法绑定到“ router-link”,因为它不是已知的本机属性 - Angular 2 Beta Routing Error: Template parse errors: Can't bind to 'router-link' since it isn't a known native property Angular2测试 - 失败:未捕获(在承诺中):错误:模板解析错误:无法绑定到“消息”,因为它不是已知的属性 - Angular2 Testing - Failed: Uncaught (in promise): Error: Template parse errors: Can't bind to 'message' since it isn't a known property of Angular 4/5 未捕获错误:模板解析错误:无法绑定到“mat-dialog-close”,因为它不是“button”的已知属性 - Angular 4/5 Uncaught Error: Template parse errors: Can't bind to 'mat-dialog-close' since it isn't a known property of 'button' 错误:模板解析错误:无法绑定到“评级”,因为它不是“评级”的已知属性 - Error: Template parse errors: Can't bind to 'rating' since it isn't a known property of 'rating' 错误:模板解析错误:无法绑定到“myProperty”,因为它不是“myComponent”的已知属性 - Error: Template parse errors: Can't bind to 'myProperty' since it isn't a known property of 'myComponent' 未捕获的错误:模板解析错误:无法绑定到“ FormGroup”,因为它不是“ form”的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'FormGroup' since it isn't a known property of 'form' 错误:模板解析错误:无法绑定到“选项”,因为它不是“图表”的已知属性 - Error: Template parse errors: Can't bind to 'options' since it isn't a known property of 'chart'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM