如何在 angular2 中的同一组件中定义自定义属性



我正在尝试定义自定义属性计数。但以下给出错误:无法绑定到"计数",因为它不是"p"的已知属性。如何删除此错误并使计数成为自定义属性<p>

其他组件.html

<p [count] = "10">
other works!
</p>

other.component.ts

import {Component, Input, OnInit} from '@angular/core';
@Component({
selector: 'app-other',
templateUrl: './other.component.html',
styleUrls: ['./other.component.css']
})
export class OtherComponent implements OnInit {
@Input() count = 10;
constructor() { }
ngOnInit() {
}
}

><p>没有name属性。 您可以使用以下方法绑定到name属性

[attr.name]="name"

或者

attr.name="{{name}}"

仅将此第二种形式(内插)用于绑定字符串值,因为传递的值将始终字符串化。

你使用@Input of Angular 是错误的。

@Input() count = 10 -> 它用于为命名"app-other"的组件设置属性。因此,您必须在父组件上设置 count 属性。

例如在app.component.html中:

<div>
<app-other [count] = 10></app-other>
</div>

希望对您有所帮助!

最新更新