Aji*_*wat 18 html css angular-template angular-directive angular
我有一个表,我想在其中显示一个表行,这是一个组件.而且我还想将数据传递给该组件:
<table>
<th>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</th>
<tr>
<my-component [data1]="data1" [data2]="data2"></my-component>
</tr>
<tr *ngFor="let item of list">
{{item}}
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
在my-component,HTML是少数几个<td></td>从data1和呈现的数据data2.
但是在渲染之后,由于<my-component></my-component>我的CSS破坏导致所有my-componentHTML(整个表行)显示在第1列中.
结果如上:
<table>
<th>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</th>
<tr>
<my-component>
<td>data1.prop1</td>
<td>data1.prop2</td>
</my-component>
</tr>
<tr *ngFor="let item of list">
{{item}}
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
@Component({
selector: '[my-component]',
templateUrl: 'my-component.html'
})
<tr my-component [data1]="data1" [data2]="data2"></tr>
Run Code Online (Sandbox Code Playgroud)
但这会导致错误Can't bind to 'data1' since it isn't a known property of 'tr'.
我也不能使用,@Directive因为我想渲染HTML.
如何渲染<my-component></my-component>没有<my-component></my-component>标签的模板?
其他答案是先前版本的角度.我正在使用Angular 4.3.4.
任何帮助,将不胜感激..!
Mad*_*jan 11
您还需要在下面的选择器中加入tr,
@Component({
selector: 'tr[my-component]',
template: `
<td>{{data1.prop1}}</td>
<td>{{data1.prop2}}</td>
<td>{{data2.prop1}}</td>
`
})
export class MyComponent {
@Input() data1;
@Input() data2;
}
Run Code Online (Sandbox Code Playgroud)
检查这个柱塞!
希望这可以帮助!!
| 归档时间: |
|
| 查看次数: |
5145 次 |
| 最近记录: |