Angular - Focus on input with dynamic IDs on click

后端 未结 2 1127
心在旅途
心在旅途 2021-01-28 13:45

*There are a lot of similar questions but I have not found a true duplicate that answers my question Apologies if I missed something.

I have a page with several inputs

2条回答
  •  借酒劲吻你
    2021-01-28 13:58

    How about to use a data-attribute with id and get the input from it?

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    // component constructor
    constructor(
        private readonly elementRef: ElementRef,
        // ...
      ) {
        // ...
      }
    
    focusOnInput(event: MouseEvent): void {
        const groupId = (event.target).dataset.group;
        const input = this.elementRef.nativeElement.querySelector(`input[data-group="${groupId}"]`);
        input.focus();
    }
    

提交回复
热议问题