Angular 5 How to do Show/Hide form field mat-select on change


Angular 5 How to do Show/Hide form field mat-select on change



I want to show or hide form field when mat-select is changed. The following code I used to do a show or hide the process. But it shows an error:



Cannot read property 'valueFieldType' of undefined.



1).html file


<mat-form-field style="width: 30%">
<mat-select formControlName="evaluationRuleField" placeholder="Select Field" [value]="evaluationRuleField" id="evaluationRuleField" name="evaluationRuleField">
<mat-option *ngFor="let evaluationRuleField of evaluationRuleFields" [value]="evaluationRuleField">{{ evaluationRuleField.viewValue }}</mat-option>
</mat-select>
</mat-form-field>
<!--Start Dynamically Change Field-->
<mat-form-field class="example-full-width" style="width: 30%" *ngIf = "evaluationRuleField.valueFieldType == 'text'">
<input matInput formControlName="evaluationRuleValue" placeholder="Value" [ngModel]="evaluationRuleValue" id="evaluationRuleValue" name="evaluationRuleValue" required>
</mat-form-field>
<mat-form-field class="example-full-width" style="width: 30%" *ngIf = "evaluationRuleField.valueFieldType == 'dropdwon'">
<mat-select formControlName="evaluationRuleField" placeholder="Select Field" [(value)]="ruleValueFields" id="evaluationRuleField" name="evaluationRuleField" (change)="getValue()">
<mat-option *ngFor="let ruleValueField of ruleValueFields" [value]="ruleValueField.value">{{ ruleValueField.viewValue }}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="example-full-width" style="width: 30%" *ngIf = "evaluationRuleField.valueFieldType == 'multiselect'">
<mat-select placeholder="Toppings" formControlName="evaluationRuleField" multiple>
<mat-option *ngFor="let topping of toppingList" [value]="topping">{{topping}}</mat-option>
</mat-select>
</mat-form-field>
<!--Start Dynamically Change Field-->



2).ts file


private fieldArray: Array<any> = [{evaluationRuleField:"",condition:"condition",value:"value"}];

evaluationRuleFields = [
{value:"field_1",valueFieldType:'text',viewValue:"Field 1"},
{value:"field_2",valueFieldType:'dropdown',viewValue:"Field 2"},
{value:"field_3",valueFieldType:'text',viewValue:"Field 3"},
{value:"field_4",valueFieldType:'multiselect',viewValue:"Field 4"},
{value:"field_5",valueFieldType:'dropdown',viewValue:"Field 5"}
] {value:"field_3",valueFieldType:'text',viewValue:"Field 3"},
{value:"field_4",valueFieldType:'multiselect',viewValue:"Field 4"},
{value:"field_5",valueFieldType:'dropdown',viewValue:"Field 5"}
]





You have a bit too many 'evaluationRuleField' variables in your code there...
– Carsten
Jul 3 at 7:00





produce a plucker
– Prashant Pimpale
Jul 3 at 7:03





(change) will give you the selected objects array
– Prashant Pimpale
Jul 3 at 7:04




2 Answers
2



Try below code:



HTML Code:


<mat-form-field>
<mat-select [(value)]="selected" formControlName="evaluationRuleField" id="evaluationRuleField" placeholder="Select value" name="evaluationRuleField">
<mat-option *ngFor="let evaluationRuleField of evaluationRuleFields" [value]="evaluationRuleField">
{{evaluationRuleField.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
{{selected}} // the selected value



Your Conditions:









{{ ruleValueField.viewValue }}




{{topping}}



{{selected | json}}



TS:


public selected: any; // which returns an array of selected value objects incase single select then returns an object



And the reason behind the undefined because when the variable is intialized then it don't have a property like valueFeildType


undefined


valueFeildType



Ex StackBlitz Demo:



https://stackblitz.com/edit/angular-dscav5?file=app%2Fselect-value-binding-example.html





Thank Prashant Pimpale.But i want to show the form field based on the object value.Like *ngIf = "evaluationRuleField.valueFieldType == 'text'"
– Satheesh
Jul 3 at 7:21





Yes then you can use selected.valueFeildType ='text'
– Prashant Pimpale
Jul 3 at 7:30





same error Cannot read property 'valueFieldType' of undefined
– Satheesh
Jul 3 at 7:36





Wait i m editing my answer
– Prashant Pimpale
Jul 3 at 7:45





thanks brother its working fine.I am expecting some helps from you in feature.
– Satheesh
Jul 3 at 9:32




You need to use ngModel so as to get the value to validate. Whatever you define in value="", will be assigned to model on selection.



Replace this :


<mat-select formControlName="evaluationRuleField" placeholder="Select Field"
[value]="evaluationRuleField" id="evaluationRuleField"
name="evaluationRuleField">
<mat-option *ngFor="let evaluationRuleField of
evaluationRuleFields" [value]="evaluationRuleField">{{
evaluationRuleField.viewValue }}</mat-option>
</mat-select>



With :


<mat-select formControlName="evaluationRuleField" placeholder="Select Field"
([ngModel])="ev"
id="evaluationRuleField"
name="evaluationRuleField">
<mat-option *ngFor="let evaluationRuleField of
evaluationRuleFields" [value]="evaluationRuleField.valueFieldType">{{
evaluationRuleField.viewValue }}</mat-option>
</mat-select>



and Then use it like :


*ngIf = "ev == yourvalue"





Thanks for the code.In this line i got error valueFieldType of undefined.<mat-select formControlName="evaluationRuleField" placeholder="Select Field" [value]="evaluationRuleField.valueFieldType" ([ngModel])="ev" id="evaluationRuleField" name="evaluationRuleField">
– Satheesh
Jul 3 at 7:14






I have updated code, just check!
– Sidhanshu_
Jul 3 at 7:16





the error is gone.But the condition is not working
– Satheesh
Jul 3 at 7:32





Can you produce a plunkr for this... so that I can see your code. Make sure you are using inverted comma in ngif condition, *ngIf="ev=='yourvalue' "
– Sidhanshu_
Jul 3 at 7:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

PHP contact form sending but not receiving emails