繁体   English   中英

在 angular 4 中从 html 中删除主机组件标签

[英]Remove host component tag from html in angular 4

我有一个表格,我想在其中显示一个表格行,它是一个组件。 而且我想将数据传递给该组件:

<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>

my-component ,HTML 是一些<td></td>其中包含从data1data2呈现的数据。

但是在渲染它之后,由于<my-component></my-component>我的 CSS 被破坏,导致所有my-component HTML(整个表格行)显示在第一列中。

以上结果:

<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>

我尝试了以下方法:

@Component({
    selector: '[my-component]',
    templateUrl: 'my-component.html'
})

<tr my-component [data1]="data1" [data2]="data2"></tr>

但这会导致错误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。 我正在使用 Angular 4.3.4。

任何帮助,将不胜感激..!

您还需要在选择器中包含 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;
}

检查这个Plunker

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM