I am creating a dynamic form where I am populating the fields dynamically based on the response from JSON like.
Eg:-
[{
"type":"text",
"required":true,
"minlength": 3,
"maxlength":5,
"name":"fname",
"visibility":true
},
{
"type":"text",
"required":true,
"minlength": 3,
"maxlength":5,
"name":"lname",
"visibility":"fname == 'abc' || fname == 'xyz'"
},
{
"type":"text",
"required":true,
"minlength": 3,
"maxlength":5,
"name":"fid",
"visibility":true
},
{
"type":"text",
"required":true,
"minlength": 3,
"maxlength":5,
"name":"lid",
"visibility":"fid == 1 || fid == 4"
}]
I have a usecase where the second field should be visible only when the first field should have the values 'abc' or 'xyz'(Condition is written in the JSON property).How can that be achieved dynamically?
Create
evaluation
method in component:And call it in template like this:
Your
json
file will be like:visibility: 'this.form.get("firstName").value ==="abc"'
, as you can see you should write injson
as usual in component class logic, because it will run in component contextCODE EXAMPLE