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:
- Created 7 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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. YourngSubmit
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.
@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