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() {
    // I want append below code whenever I click.
    <tr>
      <td style="padding: 30px">
        <input type="file" accept="image/png, image/gif, image/jpeg, image/bmp, image/x-icon" (click)="alert('$event')">
      </td>
      <td style="padding: 30px">
        <input type="file" accept="image/png, image/gif, image/jpeg, image/bmp, image/x-icon" (click)="test2(file)">
      </td>
    </tr>
    <tr>
      <td style="padding: 30px">
          <input  placeholder="image Info" type="text" size="25">
      </td>
      <td style="padding: 30px">
          <input placeholder="image Info" type="text" size="25">
      </td>
    </tr>
}

1 Answer 1

1

Here you go :

Component side :

this.rows = [];

addTbody() {
    this.rows.push(1);
}

Template side :

<table style="text-align: center; width: 100%">
    <tbody>
        <ng-container *ngFor='let row of rows'>
            <tr>
                <td style="padding: 30px">
                    <input type="file" accept="image/png, image/gif, image/jpeg, image/bmp, image/x-icon" (click)="alert('$event')">
                </td>
                <td style="padding: 30px">
                    <input type="file" accept="image/png, image/gif, image/jpeg, image/bmp, image/x-icon" (click)="test2(file)">
                </td>
            </tr>
            <tr>
                <td style="padding: 30px">
                    <input  placeholder="image Info" type="text" size="25">
                </td>
                <td style="padding: 30px">
                    <input placeholder="image Info" type="text" size="25">
                </td>
            </tr>
        </ng-container>
    </tbody>
</table>

Link to WORKING DEMO

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

12 Comments

Thanks for the reply again. But, this code occur error this, "If 'ngTemplate' is an Angular component, then verify that it is part of this module."
Sorry typo please change it to ng-template, try again.
Hmm... now error doesn't happened but nothing appended when I clicked a button.
Did you clicked on button ?
Yes, I did. What does the meaning of 1 in the this.rows.push()?
|

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.