question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Focusing on md-input-container programatically

See original GitHub issue

I have a component that has some md-input-container elements. I would like to set focus on the first one when the view loads.

The way I am implementing this is by creating a @ViewChild property and then calling focus on it ngAfterViewInit. There are no errors but, the focus is still not working.

export class AdminUserComponent implements AfterViewInit {
    @ViewChild(MdInputContainer)
    private firstInput: MdInputContainer;

    ngAfterViewInit() {
        this.firstInput._mdInputChild.focus();
    }
}

I tried setting the focus code in a setTimeout function but that didn’t make a difference. Has anyone successfully set focus on a material input when the view loads?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
vadim82commented, Feb 9, 2017

Thank you for helping me. I found my problem… I tried with MdInput and it didnt work. Then I tried with MdInputDirective and now it works perfectly.

2reactions
fxckcommented, Feb 8, 2017

I just linked you the source code of md-input-container, there is no focus() method, nor does the documentation say anything about any focus() method on the MdInputContainer https://material.angular.io/components/component/input

And it seems to be working to me when I focus the input http://plnkr.co/edit/fVQshe6iwOMpDZNd8HCH?p=preview

Read more comments on GitHub >

github_iconTop Results From Across the Web

Programmatically focus and select angular2 material design ...
You can use the focus method from the md-input component as shown in the documentation. https://material.angular.io/components/input/api
Read more >
Demos > Input - AngularJS Material
The md-input-container gives you the flexibility to display your messages using many standard angular directives. For instance, toggle the switch to the ...
Read more >
Angular 2 Material input focus not working – iTecNote
focus is a method on mdInput that lets you programmatically focus the input. You can call the myInput.focus() with myInput being something like...
Read more >
Angular2/material 2: md-input-container label is not resetting ...
Coding example for the question Angular2/material 2: md-input-container label is not resetting float when value is changed programmatically-angular.js.
Read more >
Angular Material - Quick Guide - Tutorialspoint
The md-autocomplete, an Angular Directive, is used as a special input control ... to show a container component which can be shown or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found