My approach works well for handling multiple elements with the right selector.
I had a requirement to dynamically load a popup modal from the code base and perform actions like Minimize, Maximize, Close, and Resize. To achieve this, I needed to select one opened modal to carry out operations, and here is how I solved it.
Here is the HTML snippet where I used #dynamicPopupContentPlaceHolder as the selector for the div tag.
https://i.stack.imgur.com/0Pv5d.png
Firstly, I added QueryList in the component from the angular/core library as illustrated below.
https://i.stack.imgur.com/Hq6NS.png
Finally, in the component file, I utilized QueryList (which contains an array of children) to retrieve all the children using the following method.
https://i.stack.imgur.com/D9Wkp.png
Lastly, I used the widgetSelector variable to select the last child from the array using the following technique.
let _targetSelector = this.widgetTargets.last;