0

When i click on the "Add-more-fields" button in the link below, the fields are adding only once. When I click a second time, the fields are hiding.

I need to append the fields. I mean I need to display fields, when I clicks the buttons number of times

https://stackblitz.com/edit/angular-mz6aig?file=src%2Fapp%2Fapp.component.html

3
  • your question is not clear Commented Jan 18, 2019 at 12:35
  • the sample is working as per you done code Commented Jan 18, 2019 at 12:36
  • 1
    You'd require a counter which increases when you click the button and then a loop to create as many input fields as high your counter is. Commented Jan 18, 2019 at 12:37

2 Answers 2

3

You need to use ngfor to iterate the list of items based on the number of clicks,

You need to use array to carry the ng model value to component from html.

Check this working code It may required some of changes based on your requirements

https://stackblitz.com/edit/angular-jzyfpk

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

1 Comment

Yes it is working but Add-new-heading button is also repeating number of times. Button tag is there in html twice. when i remove one button tag nothing will shows. I tried this. stackblitz.com/edit/…
0

Your code does not add fields, but change the variable tmpo from undefined to 1 when you click the first time, and from 1 to 2 the second time.

Your first *ngIf checks if the value is not 1, and undefined matches this criteria. Then you set it to 1, so it no longer matches and you go to block #temp where you show the button again, this time asking for tmpo not to be 2, which the value 1 matches. Next click you set tmpo to 2, so the first *ngIf matches again (tmpo (value:2) != 1).

So all in all you simply switch between those blocks. To add blocks, you'd need some array model to bind to.

Comments

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.