我最近开始学习Angular,以下一个非常简单的问题(我认为)是我找不到解决方案:
我想创建一个具有自定义属性而没有值的Component。像这样一个:
<h1 attribute-without-value > Text <h1>
我尝试了其他方法,但仍然没有想要的东西。我可以使用以下语法创建具有值的属性:[attr.attr-with-value]="value"
,但并非没有。这是我的一些实验(参考:https://stackblitz.com/edit/angular-sn94cl?embed=1&file=src/app/app.component.ts):
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1 {{nameAttr}} > doesn\'t work <h1>
<h1 [attr.nameAttr] > also this doesn\'t work <h1>
`,
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
nameAttr = 'attribute-without-value';
}
我该怎么办?
<h1> {{nameAttr}} <h1>
如果您不希望它具有值,则可以将nameAttr设置为''或null。
或者如果您试图将值传递给没有值的组件,那么您将不得不创建第二个组件,您可以将数据传递给该组件。Mapping component attributes without a value