This is in Angular, I want to use variable to change my conditional statement so I don't need to modify every variable and type again This is a code in input tag HTML .
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
.
public form: FormGroup;
private formBuilder: FormBuilder
.
get f() { return this.form.controls; }
.
this.form = this.formBuilder.group({
id: ['00000000-0000-0000-0000-000000000000'],
name: ['', Validators.required],
email: ['', [Validators.required, Validators.pattern("^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$")]],
oraganize: ['', Validators.required],
});
.
<input
...
[ngClass]="{'is-invalid': f.name.invalid && (f.name.dirty || f.name.touched)}"
>
So my code will change every time when there is a new input such as name, email, oraganize, etc
I want to build a function that I can pass some string and make it super nice not to modify all the time with the long line. ex:
public checkCondition(attribute: string): boolean{
return (`this.f.${attribute}.invalid && (this.f.${attribute}.dirty || this.f.${attribute}.touched)`);
}
so I can use as...
<input
...
[ngClass]="{'is-invalid': checkCondition("name")}"
>
Please, So can I do it this way or not? or does it just works with only the string, not the condition?