Skip to content
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

When using @ViewChild element.nativeElement is undefined #3922

Closed
kuncevic opened this issue Apr 6, 2017 · 13 comments
Closed

When using @ViewChild element.nativeElement is undefined #3922

kuncevic opened this issue Apr 6, 2017 · 13 comments

Comments

@kuncevic
Copy link
Contributor

kuncevic commented Apr 6, 2017

Bug, feature request, or proposal:

Bug

What is the expected behavior?

@ViewChild element.nativeElement has value

What is the current behavior?

@ViewChild element.nativeElement is undefined

@Component({
  selector: 'material-app',
  template:`
  <button #button md-raised-button>button</button>
  `
  
})
export class AppComponent {
  @ViewChild('button') button;
  ngAfterViewInit(){
    console.log(this.button);
    console.log(this.button.nativeElement); //undefined
    console.log(this.button.nativeElement.value); // obviously error here
  }

What are the steps to reproduce?

http://plnkr.co/edit/gSEsELlTAbrqvOOduSdr?p=preview

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

Need to access to an element with @ViewChild

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

Angular 4.0.1
Material beta.2
All browsers

Is there anything else we should know?

If you take out the md-raised-button from the button then this.button.nativeElement will work.

@Nikki1993
Copy link

Nikki1993 commented Apr 6, 2017

As a workaround for now, you can define Child like this

@ViewChild('sidenavContainer') private sidenavContainer: any; and then call

this.sidenavContainer._element.nativeElement

For anyone wondering.

@kuncevic
Copy link
Contributor Author

kuncevic commented Apr 7, 2017

yeah I saw that thing when I was debugging, also there is one more option is to use renderer https://angular.io/docs/js/latest/api/core/index/Renderer-class.html to manipulate with elements(not sure if it is affected by the same issue), but I ended up just by declaring a prop in my comp that I bind to the my html element prop I need to interact with.

@jelbourn
Copy link
Member

jelbourn commented Apr 9, 2017

This is working as expected. When using @ViewChild in this way, Angular will provide the directive instance.

@jelbourn jelbourn closed this as completed Apr 9, 2017
@kuncevic
Copy link
Contributor Author

kuncevic commented Apr 9, 2017

@jelbourn why it is expected?

Angular will provide the directive instance

Some more details plz

@willshowell
Copy link
Contributor

@kuncevic this should help

http://stackoverflow.com/a/39909203/4811678

@gouldina
Copy link

I also have this problem. I don't understand why you say this is OK - if it works with a button without the md-radio-button, surely it should work for one with it? How can I get around this problem?

@HunderlineK
Copy link

HunderlineK commented Oct 8, 2017

@gouldina Please refer to the link that @willshowell posted above. Basically, just do
@ViewChild('nameForMaterialElement', {read: ElementRef}) private nameForMaterialElement: ElementRef;

The reason is that md-radio-button is an angular component (which itself is a directive) and ViewChild returns the whole component if you do not specify the read option

@gouldina
Copy link

gouldina commented Oct 9, 2017

Thanks HunderlinK. I'll try that.

@chibibiscuit
Copy link

To add on to @Nikki1993's answer (which works absolutely awesome), Angular Material 6 already has an interface you can use to type it:

export interface HasElementRef {
    _elementRef: ElementRef;
}

@ViewChild('sidenavContainer') private sidenavContainer: HasElementRef;

@kennyalmendral
Copy link

Thanks @HunderlineK

@raj2033
Copy link

raj2033 commented Jun 21, 2019

down to 209, and this problem still exists.

@kuncevic
Copy link
Contributor Author

@raj2033 did you try any of solutions above?

@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

No branches or pull requests

9 participants