0

Using Angular, I am calling a function inside a .html file, which returns a string containing a table element. I want this string to convert to html code.

I have tried [innerHtml]:

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

This works, printing the table, but the problem is that inside the table I have id/classes, unique for each cell, and I am not able to access them inside the .html file using this method. Styling the table from a CSS file is also not working. And I want to avoid inline CSS.

Any suggestions how to solve this so I can access the id/classes/objects? Thanks in advance!

3

1 Answer 1

1

Maybe you don't need to insert html directly, what about this:

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

Put all your html to the template, and component class only deals with data.

And in your component:

// Cell is like this:
// {id: string; text: string}
cells: Array<Cell[]>;
ngOnInit() {
    this.cells = this.getCells();
}
getCells() {
    let cells = .........;
    return cells;
}
Sign up to request clarification or add additional context in comments.

6 Comments

I have tried, it did not work. I use a function to create the table cells, outputted as a string. So putting it inside {{ }} would only print the string, not running it as html code.
What i mean is, don't create html string(the table cells) in a function, just put all the html structure in the template, like: <table><tr><td>{{cellInfo}}</td><tr><table>, and only create the cellInfo property with functions
I can't, because I have functions to determine how many cells the table should have, not only putting a content in them.
What about *ngFor? It will render the cells you give to it dynamically, no matter how many.
You mean like <p *ngFor="let calendar of function()">{{calendar}}</p>? It doesn't show the table.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.