Skip to content

MdAutocomplete Throws error on dynamic focus #4844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hemantjadon opened this issue May 27, 2017 · 4 comments · Fixed by #4851
Closed

MdAutocomplete Throws error on dynamic focus #4844

hemantjadon opened this issue May 27, 2017 · 4 comments · Fixed by #4851
Assignees

Comments

@hemantjadon
Copy link

Bug, feature request, or proposal:

Bug: The MdAutocomplete throws the error

FeedHeaderComponent.html:12 ERROR TypeError: Cannot read property 'template' of undefined
    at MdAutocompleteTrigger._createOverlay (material.es5.js:18786)
    at MdAutocompleteTrigger.openPanel (material.es5.js:18550)
    at Object.eval [as handleEvent] (FeedHeaderComponent.html:12)
    at handleEvent (core.es5.js:11852)
    at callWithDebugContext (core.es5.js:13060)
    at Object.debugHandleEvent [as handleEvent] (core.es5.js:12648)
    at dispatchEvent (core.es5.js:8830)
    at core.es5.js:9419
    at HTMLInputElement.<anonymous> (platform-browser.es5.js:2674)
    at ZoneDelegate.invokeTask (zone.js:398)
    at Object.onInvokeTask (core.es5.js:4136)
    at ZoneDelegate.invokeTask (zone.js:397)
    at Zone.runTask (zone.js:165)
    at HTMLInputElement.ZoneTask.invoke (zone.js:460)
    at FeedComponent.webpackJsonp.183.FeedComponent.focusTextbox (feed.component.ts:67)

When I try to dynamically focus the textbox using ElementRef

ngOnInit() {
this.elementRef.nativeElement.querySelector('feed-header input#search').focus();
}

What is the expected behavior?

The MdAutocomplete should not crash when the focus is brought to the application.

What is the current behavior?

The MdAutocomplete crashes the application with the above stack trace

What are the steps to reproduce?

Providing a Plunker (or similar) is the best way to get the team to see your issue.
Plunker template: https://goo.gl/DlHd6U

http://plnkr.co/edit/4gdaiKeAon2ziXJ9AOd1

What is the use-case or motivation for changing an existing behavior?

This is the common use case for the application to have the dynamic focus when the a

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular v4.0.0

Is there anything else we should know?

@crisbeto
Copy link
Member

crisbeto commented May 28, 2017

It's throwing because you're trying to open the autocomplete too early. It'll work if you focus the input in ngAfterViewInit, instead of ngOnInit. That being said, we should be throwing a better exception.

@crisbeto crisbeto self-assigned this May 28, 2017
crisbeto added a commit to crisbeto/material2 that referenced this issue May 28, 2017
…a panel

Adds a more informative error if the user tries to open an autocomplete that doesn't exist or hasn't been defined yet by the `@ViewChild` query.

Fixes angular#4844.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 31, 2017
…a panel

Adds a more informative error if the user tries to open an autocomplete that doesn't exist or hasn't been defined yet by the `@ViewChild` query.

Fixes angular#4844.
andrewseguin pushed a commit that referenced this issue Jun 5, 2017
…a panel (#4851)

Adds a more informative error if the user tries to open an autocomplete that doesn't exist or hasn't been defined yet by the `@ViewChild` query.

Fixes #4844.
@MarkJeoffery
Copy link

I'm having the same issue. This only happens on iPad. I haven't had this on other platforms.

Any fixes apart the one above? I did try it but doesn't work.

@irrfan13
Copy link

Hi Everyone,

I am facing problem in matautocompletetrigger while using matautocomplete in viewchild in jasmine typescript code is working fine.
'undefined is not a constructor ' this the error error i am getting in karma-jasmine.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants