1

I'm trying to append elements dynamically when I click a button. Actually when I click a button, elements append well. But the problem is that function and style is not worked. My code is below.

P.S. I'm using angular5

HTML

<table style="text-align: center; width: 100%">
    <tbody #tContent>
        <!-- I want to append element in here -->
    </tbody>
</table>
    <button type="button" mat-raised-button color="primary" (click)="addTbody()">click here</button>

TS

    addTbody() {
    ???
}
1
  • Haha I know but I'm already using angular Commented Nov 16, 2017 at 3:04

1 Answer 1

3

Here it is, In the Angular way:

Component side

rowData = [];

addTbody() {
    rowData.push({
        name : 'something',
        age : '15',
        ...
    });
}

Template side

<table style="text-align: center; width: 100%">
    <tbody>
        <tr *ngFor='let row of rowData'>
            <td> {{ row.name }} </td>
            <td> {{ row.age }} </td>
        </tr>
    </tbody>
</table>

<button type="button" mat-raised-button color="primary" (click)="addTbody()">click here</button>

Try to avoid jQuery as much as possible,while learning Angular

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. What if I want to append input field instead of data, what should I do? My question was it actually.
if I append input field, then it doesn't work well. I edited my post. Please check it out.
@MingyuJeon, in that case you need to read : angular.io/guide/dynamic-form
I reverted and asked another question. Here's the link. TY stackoverflow.com/questions/47322274/…

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.