How to change the visibility of an input by selecting an item in select and not changing all in ngFor

Asked

Viewed 63 times

0

When you select the 'title' item it is to hide the input. But when I select another item from another ngFor index, the input selected 'title' is displayed again. Example

1 answer

2


The problem is that you are doing this with a whole type variable:

idvalue = 0;

To solve your problem, create an array to receive the value of each line:

idvalue = [];

And change your method:

changeValue(event, index) {
    console.log(event, index);
    this.evento = event === index + '_title';
    this.idvalue = index;
}

To:

changeValue(event, index) {
    this.idvalue[index] = event.includes('title') ? index : -1;
}

This way we check if the selected option is "title" and assign the value at the position of the array corresponding to the line you want to hide the field.

And finally you no longer need the "event" option in your input:

<input [hidden]="i === idvalue[i]" id="id_{{i}}" name="name_{{1}}" type="text" value="">

Upshot

  • Got it! Thanks for the details in the explanation. It worked perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.