HTML代码显示为字符串

时间:2016-10-10 17:19:48

标签: html angular typescript

使用Angular,我在.html文件中调用一个函数,它返回一个包含表元素的字符串。我希望这个字符串转换为HTML代码。

我试过[innerHtml]:

<p [innerHtml]="kalenderen(forsteUkedag('2016-08-01' | date:'E'), 8, 2016)"></p>

这有效,打印表,但问题是在表内我有id / classes,每个单元格都是唯一的,我无法使用此方法在.html文件中访问它们。从CSS文件设置表格样式也不起作用。我想避免使用内联CSS。

任何建议如何解决这个问题,以便我可以访问id / classes / objects? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

也许你不需要直接插入html,这是怎么回事:

<p>
    <table id="yourId" class="yourClass">
        <tr>
            <td *ngFor="let cell of cells" 
                [attr.id]="cell.id">{{cell.text}}</td>
        </tr>
    </table>
</p>

将所有html放到模板中,组件类只处理数据。

在你的组件中:

// Cell is like this:
// {id: string; text: string}
cells: Array<Cell[]>;
ngOnInit() {
    this.cells = this.getCells();
}
getCells() {
    let cells = .........;
    return cells;
}