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
Angular2: Cannot read property 'nativeElement' of undefined #411
Comments
I've traced the problem down to this one line:
Using Moving the This worked fine when using the |
Investigated this some more via input from the Angular 2 team on angular/angular#6179 and using this Plunkr. It seems to work fine with Angular 2 components, but not with angular2-polymer or vaadin-grid elements. Perhaps angular2-polymer is not handling resolution of the components in the view before |
This issue affects all elements, as you can see with this snippet: <input type="checkbox" [checked]="visible" (change)="visible=$event.target.checked">
<div *ngIf="visible">
<label #hiddenLabel>Visible!</label>
</div> export class AppComponent {
@ViewChild('hiddenLabel') label: ElementRef;
private visible: Boolean;
constructor() {
this.visible = false;
}
ngAfterViewInit() {
console.log(this.label.nativeElement); // throws an error
}
} Your Plunkr example works in this case because Indeed I'd suggest either using |
As we are no longer actively supporting the use of our elements with Angular, I’m closing this issue. Please open a new issue in the angular-polymer project if you are still affected by this issue. |
I'm getting "Cannot read property 'nativeElement' of undefined" but only after I save to a firebase backend.
`export class HomePage implements OnInit{ @ViewChild('search') public searchElement: ElementRef; ` ngOnInit(){
` savePersonalInfo() {
After I save the data to firebase, when I navigate back to the page with the autofill I get the above mentioned error. I have to close the app then re-open in order for it to resume working. |
Using openModal type of method where modal has *ngIf condition will set the native element to undefined as the native element is only created when the condition mentioned with *ngIf becomes true. But there is a way around this if you absolutely have to use *ngIf by using Javascript's event loop manipulation using setTimeout or Promises or any sort of callbacks. Here's an example HTML Code
TS Code
Now you won't get the 'nativeElement undefined error' even with *ngIf. Why does this work? It is because of the angular/JS event loop. Basically *ngIf ensures that the modal with the id as 'sampleModal' only comes into the DOM as a native element when the boolean 'showModal' becomes true. Therefore, the command for actually showing the Modal which is the 'showHideModal' function in the given code must be a part of the next loop. While the openModal function is running, the setTimeout will set its calling function at the end of the loop, i.e. after the current function call stack is over. Therefore, we also need to pass in the context of the showHideModal function for it to run in the given scope. So, we use a variable 'that' and set it to the execution context 'this' of the class in which it is defined and pass that.showHideModal in the setTimeout. So, by the time setTimeout executes its callback function, the *ngIf condition has become true and the modal is present in the DOM to be shown or hidden by the 'showHideModal' function. |
Seeing as this is the first google result for this generic issue I would like to point out that the fix for me was to change target back to es5 from es6 related to angular/components#13695 |
TypeError: Cannot read property 'nativeElement' of undefined C'est l'erreur que je rencontre dans l’exécution de mon code import { Component, OnInit, ViewChild } from '@angular/core'; import { LoadingController } from '@ionic/angular'; @component({ constructor( } ngOnInit() {
} async loadMap() {
} //Script pour afficher la position de l'utilisateur à l'aide d'un marker sur la carte;
} async transportOptions() { } |
…rride Prevent overriding text when clear icon is shown
This was working fine with 1.1.0-beta4 using
(grid-ready)="onGridReady($event)"
event. Now that I've moved to 1.1.0 final, I can't seem to get the grid in a good way. It seems like thegrid-ready
event was removed?So I've been trying to use the
@ViewChild('grid')
approach withngAfterViewInit()
, but that isn't working. I've triedngAfterContentInit()
as well w/ the same result.Using:
"vaadin-grid": "1.1.0",
"@vaadin/angular2-polymer": "1.0.0-beta2",
"@angular/common": "2.0.0-rc.4",
"@angular/compiler": "2.0.0-rc.4",
"@angular/core": "2.0.0-rc.4",
"@angular/forms": "0.2.0",
"@angular/http": "2.0.0-rc.4",
"@angular/platform-browser": "2.0.0-rc.4",
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
"@angular/platform-server": "2.0.0-rc.4",
"@angular/router": "3.0.0-beta.2",
Template:
Component:
Always results in:
The text was updated successfully, but these errors were encountered: