use*_*909 4 javascript components angular
我使用最新版本的angluar。(7.2.0) 我有个人 tr 组件,如:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-table-row',
templateUrl: './table-row.component.html',
styleUrls: ['./table-row.component.scss']
})
export class TableRowComponent implements OnInit {
@Input() character: any;
@Input() columns: string[];
constructor() { }
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
我想在表中使用这个组件,如:
<table class="mat-elevation-z8">
<tr>
<th *ngFor="let c of columns">{{c}}</th>
</tr>
<tr app-table-row class="component-style table-row-component" *ngFor="let ch of characters | async"
[character]="ch"
[columns]="columns">
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
得到如下错误:
Can't bind to 'character' since it isn't a known property of 'tr'. ("table-row class="component-style table-row-component" *ngFor="let ch of characters | async"
[ERROR ->][character]="ch"
[columns]="columns">
</tr>
"): ng:///AppModule/TableComponent.html@7:7
Run Code Online (Sandbox Code Playgroud)
如何在表格中正确添加我的组件?
将组件选择器定义为带有方括号的属性选择器:
@Component({
selector: 'tr[app-table-row]',
...
})
Run Code Online (Sandbox Code Playgroud)
以便可以将其设置为tr元素的属性:
<tr app-table-row ...>
Run Code Online (Sandbox Code Playgroud)