'Selected item from array after delete is still selected
I have form for selecting nodeId from array of all nodes.
<select id="nodeId" formControlName="nodeId">
<option *ngFor="let nodeId of allNodesId" [ngValue]="nodeId">{{nodeId}}</option>
</select>
For example allNodesId contains values: 'a' , 'b' and 'c'.
If value 'a' is selected in this form and after that in other part of program value 'a' will be removed from array allNodesId, than still this.myFormName.get('nodeId')!.value === 'a'
I resolved that issue by first checking if element which will be deleted is currently selected and if is than this.myFormName.get('nodeId')!.setValue(this.allNodesId.length > 0 ? this.allNodesId[0] : '');
What if a node could be deleted in many parts of the program? Then in all those functions, I will have to remember and add this line of code.
I'd like to take responsibility for setting selection if the currently selected node will be deleted from a function that will execute deletion.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
