我正在尝试定义自定义属性count。但以下给出错误:无法绑定(bind)到“count”,因为它不是“p”的已知属性。如何消除此错误并使 count 成为 <p>
的自定义属性
other.component.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
属性(property)。
您可以绑定(bind)到 name
属性使用
[attr.name]="name"
或者替代地
attr.name="{{name}}"
仅将此第二种形式(插值)用于绑定(bind)字符串值,因为传递的值将始终被字符串化。
关于angular - 如何在angular2中的同一组件中定义自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43898344/