Angular2 using button to dynamically add form elements?

7,889 views
Skip to first unread message

Robert F

unread,
May 29, 2016, 10:37:12 PM5/29/16
to AngularJS
Hi all-

I'm trying to put together an angular 2 app that has as part of a form the ability to dynamically add another row of input fields, in this case just 2-3 text fields per row.    
Ideally the user would click on an 'add row' button that would be just below the current last row and another row of text input fields would be added.

I'd appreciate any advice and pointers to any sample code that performs the above in Angular2.

New to the group, new to Angular2, and have tried searching for an example; both in this group as well as the general interwebs...

Thanks in advance

Manish Pal

unread,
May 30, 2016, 2:11:40 AM5/30/16
to AngularJS
A general idea, agnostic of framework....

each row that you have in your form is a visual representation of a record in a collection. So start by creating a component, which represents a row, and the parent component can then just show you a list of those records using ngFor (in case of Angular 2) and add row would simply become adding a new record to the collection.

Sander Elias

unread,
May 30, 2016, 3:01:06 AM5/30/16
to AngularJS
Hi Robert,

Here is a quick sample on how to add a row. For the edit part and so on, you can add one or more extra components, but I think this will get you started..

Regards
Sander


Reply all
Reply to author
Forward
0 new messages