Add component to dom on click of button in angular2

前端 未结 1 946
既然无缘
既然无缘 2020-12-31 12:42

I have personInvolved component. This component has personDetails component. There is a button in personInvolved component. Onclick of the button I need to append the person

相关标签:
1条回答
  • 2020-12-31 13:05

    Use *ngFor:

        <button (click)="addPerson()">Add person</button>
        <person-details *ngFor="let person of persons" [person]="person"></person-details>
    

    And in the component code:

        persons: Array<Person> = [];
    
        addPerson() {
            this.persons.push(new Person());
        } 
    
    0 讨论(0)
提交回复
热议问题