Angular ngIf formGroup












0














I have a form on Angular that allows to display an input according to the value selected in a drop-down list.



Here is an example of my code:



(If two is selected an input appears)



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



If I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"



My html:



 <div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select [(ngModel)]="selectedOption" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="selectedOption == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


My component.ts:



import { Component, OnInit } from '@angular/core';

import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
selector: 'app-create-us',
templateUrl: './create-us.component.html',
styleUrls: ['./create-us.component.css']
})
export class CreateUsComponent implements OnInit {

public usForm: FormGroup;
public selectedOption: string;

constructor(private fb: FormBuilder) {
}

ngOnInit() {
this.createForm();
}

createForm() {
this.usForm = this.fb.group({
'type': [null, ],
'appears': [null, ],
});
}

options = [
{ name: 'first', value: 1 },
{ name: 'two', value: 2 }
];
}


I simplified my problem as much as in reality it is in a large form with a dozen input



I need your help, thanks in advance



Regards,
Valentin










share|improve this question
























  • I didn't quite understand what you meant in line "It does not work if I leave [formGroup] = "usForm" of my form tag. Because the ngIf is not executed"
    – SiddAjmera
    Nov 19 '18 at 13:39










  • Thank you for your answer. Bad if I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"
    – Valentin
    Nov 19 '18 at 13:44










  • Duplicate of (stackoverflow.com/questions/39152071/…)? The rest of the code may be of good use
    – Nuno
    Nov 19 '18 at 13:48










  • @Nuno Thank you for your answer, I also came across this question but it does not answer my problem
    – Valentin
    Nov 19 '18 at 13:50
















0














I have a form on Angular that allows to display an input according to the value selected in a drop-down list.



Here is an example of my code:



(If two is selected an input appears)



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



If I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"



My html:



 <div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select [(ngModel)]="selectedOption" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="selectedOption == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


My component.ts:



import { Component, OnInit } from '@angular/core';

import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
selector: 'app-create-us',
templateUrl: './create-us.component.html',
styleUrls: ['./create-us.component.css']
})
export class CreateUsComponent implements OnInit {

public usForm: FormGroup;
public selectedOption: string;

constructor(private fb: FormBuilder) {
}

ngOnInit() {
this.createForm();
}

createForm() {
this.usForm = this.fb.group({
'type': [null, ],
'appears': [null, ],
});
}

options = [
{ name: 'first', value: 1 },
{ name: 'two', value: 2 }
];
}


I simplified my problem as much as in reality it is in a large form with a dozen input



I need your help, thanks in advance



Regards,
Valentin










share|improve this question
























  • I didn't quite understand what you meant in line "It does not work if I leave [formGroup] = "usForm" of my form tag. Because the ngIf is not executed"
    – SiddAjmera
    Nov 19 '18 at 13:39










  • Thank you for your answer. Bad if I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"
    – Valentin
    Nov 19 '18 at 13:44










  • Duplicate of (stackoverflow.com/questions/39152071/…)? The rest of the code may be of good use
    – Nuno
    Nov 19 '18 at 13:48










  • @Nuno Thank you for your answer, I also came across this question but it does not answer my problem
    – Valentin
    Nov 19 '18 at 13:50














0












0








0







I have a form on Angular that allows to display an input according to the value selected in a drop-down list.



Here is an example of my code:



(If two is selected an input appears)



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



If I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"



My html:



 <div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select [(ngModel)]="selectedOption" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="selectedOption == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


My component.ts:



import { Component, OnInit } from '@angular/core';

import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
selector: 'app-create-us',
templateUrl: './create-us.component.html',
styleUrls: ['./create-us.component.css']
})
export class CreateUsComponent implements OnInit {

public usForm: FormGroup;
public selectedOption: string;

constructor(private fb: FormBuilder) {
}

ngOnInit() {
this.createForm();
}

createForm() {
this.usForm = this.fb.group({
'type': [null, ],
'appears': [null, ],
});
}

options = [
{ name: 'first', value: 1 },
{ name: 'two', value: 2 }
];
}


I simplified my problem as much as in reality it is in a large form with a dozen input



I need your help, thanks in advance



Regards,
Valentin










share|improve this question















I have a form on Angular that allows to display an input according to the value selected in a drop-down list.



Here is an example of my code:



(If two is selected an input appears)



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



If I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"



My html:



 <div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select [(ngModel)]="selectedOption" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="selectedOption == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


My component.ts:



import { Component, OnInit } from '@angular/core';

import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
selector: 'app-create-us',
templateUrl: './create-us.component.html',
styleUrls: ['./create-us.component.css']
})
export class CreateUsComponent implements OnInit {

public usForm: FormGroup;
public selectedOption: string;

constructor(private fb: FormBuilder) {
}

ngOnInit() {
this.createForm();
}

createForm() {
this.usForm = this.fb.group({
'type': [null, ],
'appears': [null, ],
});
}

options = [
{ name: 'first', value: 1 },
{ name: 'two', value: 2 }
];
}


I simplified my problem as much as in reality it is in a large form with a dozen input



I need your help, thanks in advance



Regards,
Valentin







html angular forms typescript ngif






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:50

























asked Nov 19 '18 at 13:37









Valentin

296




296












  • I didn't quite understand what you meant in line "It does not work if I leave [formGroup] = "usForm" of my form tag. Because the ngIf is not executed"
    – SiddAjmera
    Nov 19 '18 at 13:39










  • Thank you for your answer. Bad if I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"
    – Valentin
    Nov 19 '18 at 13:44










  • Duplicate of (stackoverflow.com/questions/39152071/…)? The rest of the code may be of good use
    – Nuno
    Nov 19 '18 at 13:48










  • @Nuno Thank you for your answer, I also came across this question but it does not answer my problem
    – Valentin
    Nov 19 '18 at 13:50


















  • I didn't quite understand what you meant in line "It does not work if I leave [formGroup] = "usForm" of my form tag. Because the ngIf is not executed"
    – SiddAjmera
    Nov 19 '18 at 13:39










  • Thank you for your answer. Bad if I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"
    – Valentin
    Nov 19 '18 at 13:44










  • Duplicate of (stackoverflow.com/questions/39152071/…)? The rest of the code may be of good use
    – Nuno
    Nov 19 '18 at 13:48










  • @Nuno Thank you for your answer, I also came across this question but it does not answer my problem
    – Valentin
    Nov 19 '18 at 13:50
















I didn't quite understand what you meant in line "It does not work if I leave [formGroup] = "usForm" of my form tag. Because the ngIf is not executed"
– SiddAjmera
Nov 19 '18 at 13:39




I didn't quite understand what you meant in line "It does not work if I leave [formGroup] = "usForm" of my form tag. Because the ngIf is not executed"
– SiddAjmera
Nov 19 '18 at 13:39












Thank you for your answer. Bad if I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"
– Valentin
Nov 19 '18 at 13:44




Thank you for your answer. Bad if I leave the [formGroup] = "usForm" the input display does not work. On the other hand if I delete [formGroup] = "usForm" of the tag form my code works as I want. So the problem is related to [formGroup] = "usForm"
– Valentin
Nov 19 '18 at 13:44












Duplicate of (stackoverflow.com/questions/39152071/…)? The rest of the code may be of good use
– Nuno
Nov 19 '18 at 13:48




Duplicate of (stackoverflow.com/questions/39152071/…)? The rest of the code may be of good use
– Nuno
Nov 19 '18 at 13:48












@Nuno Thank you for your answer, I also came across this question but it does not answer my problem
– Valentin
Nov 19 '18 at 13:50




@Nuno Thank you for your answer, I also came across this question but it does not answer my problem
– Valentin
Nov 19 '18 at 13:50












2 Answers
2






active

oldest

votes


















1














You should be using formControlName instead of [(ngModel)].



And then in comparison, you should be comparing to usForm.value.type instead of the selectedValue.



Give this a try:



<div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select formControlName="type" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="usForm.value.type == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


Here's a Sample StackBlitz for your ref.






share|improve this answer





















  • Thank you very much it works! I will look at the code;)Thank you very much it works! I will look at the code;)
    – Valentin
    Nov 19 '18 at 13:54



















0














Your template is loaded before form group is created. Add ngIf to wail while form group will be created:



<div class="offset-md-2" *ngIf="usForm">
<form [formGroup]="usForm">





share|improve this answer





















  • that never really happens you know.
    – SiddAjmera
    Nov 19 '18 at 13:46










  • I just tried but it does not work. Thank you for your help, I continue my research
    – Valentin
    Nov 19 '18 at 13:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375838%2fangular-ngif-formgroup%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You should be using formControlName instead of [(ngModel)].



And then in comparison, you should be comparing to usForm.value.type instead of the selectedValue.



Give this a try:



<div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select formControlName="type" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="usForm.value.type == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


Here's a Sample StackBlitz for your ref.






share|improve this answer





















  • Thank you very much it works! I will look at the code;)Thank you very much it works! I will look at the code;)
    – Valentin
    Nov 19 '18 at 13:54
















1














You should be using formControlName instead of [(ngModel)].



And then in comparison, you should be comparing to usForm.value.type instead of the selectedValue.



Give this a try:



<div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select formControlName="type" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="usForm.value.type == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


Here's a Sample StackBlitz for your ref.






share|improve this answer





















  • Thank you very much it works! I will look at the code;)Thank you very much it works! I will look at the code;)
    – Valentin
    Nov 19 '18 at 13:54














1












1








1






You should be using formControlName instead of [(ngModel)].



And then in comparison, you should be comparing to usForm.value.type instead of the selectedValue.



Give this a try:



<div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select formControlName="type" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="usForm.value.type == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


Here's a Sample StackBlitz for your ref.






share|improve this answer












You should be using formControlName instead of [(ngModel)].



And then in comparison, you should be comparing to usForm.value.type instead of the selectedValue.



Give this a try:



<div class="offset-md-2">
<form [formGroup]="usForm">
<div class="div-champs">
<p id="champs">Type
<span id="required">*</span>
</p>
<div class="select-style ">
<select formControlName="type" name="type">
<option style="display:none">
<option [value]="o.name" *ngFor="let o of options">
{{o.name}}
</option>
</select>
</div>
</div>
<p id="champs" *ngIf="usForm.value.type == 'two'">Appears
<input type="appears" class="form-control" name="appears">
</p>
</form>
</div>


Here's a Sample StackBlitz for your ref.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 13:51









SiddAjmera

13k31137




13k31137












  • Thank you very much it works! I will look at the code;)Thank you very much it works! I will look at the code;)
    – Valentin
    Nov 19 '18 at 13:54


















  • Thank you very much it works! I will look at the code;)Thank you very much it works! I will look at the code;)
    – Valentin
    Nov 19 '18 at 13:54
















Thank you very much it works! I will look at the code;)Thank you very much it works! I will look at the code;)
– Valentin
Nov 19 '18 at 13:54




Thank you very much it works! I will look at the code;)Thank you very much it works! I will look at the code;)
– Valentin
Nov 19 '18 at 13:54













0














Your template is loaded before form group is created. Add ngIf to wail while form group will be created:



<div class="offset-md-2" *ngIf="usForm">
<form [formGroup]="usForm">





share|improve this answer





















  • that never really happens you know.
    – SiddAjmera
    Nov 19 '18 at 13:46










  • I just tried but it does not work. Thank you for your help, I continue my research
    – Valentin
    Nov 19 '18 at 13:47
















0














Your template is loaded before form group is created. Add ngIf to wail while form group will be created:



<div class="offset-md-2" *ngIf="usForm">
<form [formGroup]="usForm">





share|improve this answer





















  • that never really happens you know.
    – SiddAjmera
    Nov 19 '18 at 13:46










  • I just tried but it does not work. Thank you for your help, I continue my research
    – Valentin
    Nov 19 '18 at 13:47














0












0








0






Your template is loaded before form group is created. Add ngIf to wail while form group will be created:



<div class="offset-md-2" *ngIf="usForm">
<form [formGroup]="usForm">





share|improve this answer












Your template is loaded before form group is created. Add ngIf to wail while form group will be created:



<div class="offset-md-2" *ngIf="usForm">
<form [formGroup]="usForm">






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 13:44









alexey28

4,19511221




4,19511221












  • that never really happens you know.
    – SiddAjmera
    Nov 19 '18 at 13:46










  • I just tried but it does not work. Thank you for your help, I continue my research
    – Valentin
    Nov 19 '18 at 13:47


















  • that never really happens you know.
    – SiddAjmera
    Nov 19 '18 at 13:46










  • I just tried but it does not work. Thank you for your help, I continue my research
    – Valentin
    Nov 19 '18 at 13:47
















that never really happens you know.
– SiddAjmera
Nov 19 '18 at 13:46




that never really happens you know.
– SiddAjmera
Nov 19 '18 at 13:46












I just tried but it does not work. Thank you for your help, I continue my research
– Valentin
Nov 19 '18 at 13:47




I just tried but it does not work. Thank you for your help, I continue my research
– Valentin
Nov 19 '18 at 13:47


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375838%2fangular-ngif-formgroup%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$