I am dynamically generating formcontrols in the .html file.
<div *ngFor="let question of questions" style=" width: 40%;">
<app-question [question]="question" [formGroup]="ratingForm"></app-question>
</div>
However, I need to dynamically be able to add css classes to two of the formcontrols that are auto-generated in the html file. I think I need to use @ViewChild to find the formcontrol after it's been auto-generated and then modify it to add the CSS Class I want conditionally. I only want to add the css classes if one of the two formcontrol's value have been changed.
So I have two form controls. Roof Name and Roof Year. If Roof Name OR Roof Year value changes, add a Css Class .highlight to both of them. How do I add this in the typescript? I have an OnControlChange method that triggers every time a field has been changed.
questions: any[];
ngOnInit() {
let formControl = new RatingFormElements();
this.formNames = Object.getOwnPropertyNames(formControl);
let tempQuestions: any[] =
this.questionService.getQuestionsByKeys(this.formNames);
this.questions = tempQuestions; //this becomes a formGroup
}
private onControlChange(value: any, name: string) {
if (name == "roofyear" || name == "roofname") {
//I need to do something here
//something like this:
this.questions.controls["roofname"].cssClass = "highlight"
//or something like this:
//Select DOM Element by ID: roofyear
//add CssClass highlight to it
}
}
}
This is the html that gets auto-generated.
<input _ngcontent-egw-c322="" matinput="" oninput="this.value = this.value.toUpperCase()" uppercasepipe="" class="mat-input-element mat-form-field-autofill-control ng-tns-c78-77 cdk-text-field-autofill-monitored ng-dirty ng-touched ng-valid" ng-reflect-placeholder="Roof Year" ng-reflect-name="roofyear" autocomplete="on" ng-reflect-mask-expression="0000" ng-reflect-thousand-separator="" ng-reflect-suffix="" ng-reflect-prefix="" ng-reflect-id="roofyear" type="text" ng-reflect-type="text" id="roofyear" placeholder="Roof Year" aria-invalid="false" aria-required="false">
it has id="roofyear" so all my code needs to do is be able to find this DOM element by ID and then add a css class to it.