Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

Angular 8 - focus() to not work on Google Chrome

I have been browsing around for a solution to my problem but I couldn't find any that works for me.

I have simple input box in my search.html template:

<input id="search-filter" #search type="search">

and I am trying to set the focus like this in my component search.ts:


export class SearchFilterComponent {
  @ViewChild('search', null) search: ElementRef;

  constructor() { }

  ngAfterViewInit() {
    this.search.nativeElement.focus();
  }
}

it works on IE and Firefox but not in Google Chrome. I have also tried to add a timeout as mentioned in other forum but it is still not working.

I have also tried from the Developer tools, F12 in the browser and typed:

window.setTimeout(function ()
    {
        document.getElementById('search-filter').focus();
    }, 200);

or the above without timeout - nothing happens. It's strange as when I do it from the console everything has been defintely loaded and rendered

Any suggestions? Thanks

question from:https://stackoverflow.com/questions/65883508/angular-8-focus-to-not-work-on-google-chrome

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...