question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular does not exclude ngIf elements from being sent as part of form data.

See original GitHub issue

[X] bug report

Angular does not exclude ngIf elements from being sent as part of form data.

View:

                      <ion-item *ngIf="storeData.taxbyCounty">          
                          <ion-label floating color="primary">County</ion-label>
                          <ion-input type="text" formControlName="county"></ion-input>
                      </ion-item>             
                      <ion-item *ngIf="storeData.taxbyState">          
                          <ion-label floating color="primary">State</ion-label>
                          <ion-input type="text" formControlName="state"></ion-input>
                      </ion-item>
                      <ion-item *ngIf="storeData.taxbyZip">          
                          <ion-label floating color="primary">Zip</ion-label>
                          <ion-input type="text" formControlName="zip"></ion-input>
                      </ion-item>                  
                      <ion-item *ngIf="storeData.taxbyCountry">          
                          <ion-label floating color="primary">Country</ion-label>
                          <ion-input type="text" formControlName="country"></ion-input>
                      </ion-item>                  

Component initTax(county,state,zip,country){ return this.fb.group({ ‘county’: [county,Validators.required], ‘state’:[state,Validators.required], ‘zip’: [zip], ‘country’: [country,Validators.required]
}); }

Form Data being sent upon submit:

taxbyCountry:true taxbyCounty:true taxbyState:true taxbyZip:false … country:“US” county:“Salt” state:“UT” zip:“84116”

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tboschcommented, Dec 27, 2016

@sudharsanmit ngIf only changes how your forms is displayed, but not what data it contains. I.e. the data controls the view, but not the other way around. Your ngSubmit callback will still be fired with the same data.

Regarding reactive forms: Angular has 2 kind of forms, “template driven” and “Reactive forms”. you are using the “Reactive Forms” approach, see https://angular.io/docs/ts/latest/cookbook/form-validation.html.

This is works as intended.

1reaction
tytskyicommented, Dec 20, 2016

@sudharsanmit i think [ngIf] is simply view directive, which has nothing to do with reactive form.

There is a potential solution for your use case: https://plnkr.co/edit/NfHNJgTvtq38Wil1l39x?p=preview

Read more comments on GitHub >

github_iconTop Results From Across the Web

NgIf - Angular
A structural directive that conditionally includes a template based on the value of an expression coerced to Boolean. When the expression evaluates to...
Read more >
HTML check variable undefined with ngIf excluding case ...
I would like to check this case with only condition to shorten it and make it cleaner. Is there a simple way to...
Read more >
Angular ngIf: Complete Guide
In this post, we are going to cover all the features that we have available for using the Angular ngIf core directive.
Read more >
Working with Angular 4 Forms: Nesting and Input Validation
Forms in Angular applications can aggregate the state of all inputs that are under that form and provide an overall state like the...
Read more >
Displaying data in views - Angular
Angular components form the data structure of your application. ... That element is a placeholder in the body of your index.html file:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found