spirosikmd / angular2-focus

Angular attribute directive that gives focus on an element depending on a given expression 🔎
MIT License
21 stars 2 forks source link

Focus directive breaks scroll #46

Open tamtakoe opened 6 years ago

tamtakoe commented 6 years ago

I can't give you example, because it is very specific. I used focus directive in the cell of infinity scroll table and it corrupted scroll. The same directive has same problem. Fortunately, there is good decision:

setTimeout(() => {
    this.giveFocus();
})

Use setTimeout! I made several components with focus (oi.select one of them) and I had same problems. In Angular, React, Vanilla etc. and I understood the main thing - newer use set focus without setTimeout. This is one of rare places where its use is justified

spirosikmd commented 6 years ago

Hi @tamtakoe! Thank you for submitting this issue. It would be great to provide a reproducible example. I hesitate to use setTimeout by default, but I will give it a try. Again an example would definitely help a lot!

spirosikmd commented 6 years ago

Hey, @tamtakoe any update on this one? Is this still an issue? If yes, can you please a reproducible example?

m-te commented 6 years ago

maybe try setting focus on after view checked hook? I am still new to angular so I don't know much, but from what I found, the view might not be ready yet

spirosikmd commented 6 years ago

Hi @m-te! Have you experienced the same issue?

m-te commented 6 years ago

Hi, nope, I was just looking around for a way to set focus on input, so I came across this, I eventually ended up using the #viewchild, element's focus() and ngAfterViewChecked way, using the nativeElement.focus() on ngOninit and directive didn't work, but afterViewChecked seems to be doing what I need it to do.