ionic-framework: Ionic v2 - Loading Service - Throwing error "cannot read property 'nativeElement' ..."
Short description of the problem:
I have a welcome page that invokes the Loading service in its constructor. As soon as the page loads for the first time the error / exception is thrown.
What behavior are you expecting?
No exceptions thrown.
Steps to reproduce:
- Create a new ionic project
ionic start MyIonic2Project tutorial --v2 --ts - On the root page, add in its constructor the following:
import {Page,Loading,NavController} from 'ionic-angular';
@Page({
templateUrl: 'build/pages/hello-ionic/hello-ionic.html'
})
export class HelloIonicPage {
constructor(private nav: NavController) {
this.nav =nav;
let loading = Loading.create({
content: "Please wait...",
duration: 3000,
dismissOnPageChange: true
});
this.nav.present(loading);
}
}
3 . Check the console for the exception
Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc)
browser_adapter.ts:73 STACKTRACE:BrowserDomAdapter.logError @ browser_adapter.ts:73ExceptionHandler.call @ exception_handler.ts:52(anonymous function) @ application_ref.ts:258schedulerFn @ async.ts:127SafeSubscriber.__tryOrUnsub @ Subscriber.js:166SafeSubscriber.next @ Subscriber.js:115Subscriber._next @ Subscriber.js:74Subscriber.next @ Subscriber.js:51Subject._finalNext @ Subject.js:124Subject._next @ Subject.js:116Subject.next @ Subject.js:73EventEmitter.emit @ async.ts:113NgZone._zoneImpl.ng_zone_impl_1.NgZoneImpl.onError @ ng_zone.ts:134NgZoneImpl.inner.inner.fork.onHandleError @ ng_zone_impl.ts:88ZoneDelegate.handleError @ angular2-polyfills.js:336Zone.runGuarded @ angular2-polyfills.js:244NgZoneImpl.runInner @ ng_zone_impl.ts:97NgZone.run @ ng_zone.ts:204NavController._afterTrans @ nav-controller.js:1003(anonymous function) @ nav-controller.js:979Animation._didFinish @ animation.js:590Animation.play @ animation.js:264(anonymous function) @ nav-controller.js:990ZoneDelegate.invoke @ angular2-polyfills.js:332Zone.run @ angular2-polyfills.js:227NgZoneImpl.runOuter @ ng_zone_impl.ts:98NgZone.runOutsideAngular @ ng_zone.ts:218NavController._beforeTrans @ nav-controller.js:948NavController._postRender @ nav-controller.js:930(anonymous function) @ nav-controller.js:865(anonymous function) @ nav-controller.js:1217promiseReactionJob @ es6-shim.js:2171(anonymous function) @ es6-shim.js:2159Item.run @ browser.js:64drainQueue @ browser.js:34ZoneDelegate.invokeTask @ angular2-polyfills.js:365NgZoneImpl.inner.inner.fork.onInvokeTask @ ng_zone_impl.ts:54ZoneDelegate.invokeTask @ angular2-polyfills.js:364Zone.runTask @ angular2-polyfills.js:263ZoneTask.invoke @ angular2-polyfills.js:431
browser_adapter.ts:73 TypeError: Cannot read property 'nativeElement' of undefined
at new LoadingMdPopOut (http://localhost:8101/build/js/app.bundle.js:44356:40)
at Function.Transition.createTransition (http://localhost:8101/build/js/app.bundle.js:63476:16)
at http://localhost:8101/build/js/app.bundle.js:46769:58
at ZoneDelegate.invoke (http://localhost:8101/build/js/angular2-polyfills.js:332:29)
at Zone.run (http://localhost:8101/build/js/angular2-polyfills.js:227:44)
at NgZoneImpl.runOuter (http://localhost:8101/build/js/app.bundle.js:26898:71)
at NgZone.runOutsideAngular (http://localhost:8101/build/js/app.bundle.js:26817:80)
at Portal.NavController._beforeTrans (http://localhost:8101/build/js/app.bundle.js:46759:20)
at Portal.NavController._postRender (http://localhost:8101/build/js/app.bundle.js:46741:14)
at Portal.NavController._render (http://localhost:8101/build/js/app.bundle.js:46657:18)
------------- Elapsed: 77 ms; At: Sun Apr 17 2016 13:54:49 GMT-0400 (EDT) -------------
at Object.Zone.longStackTraceZoneSpec.onScheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:1354:23)
at ZoneDelegate.scheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:342:50)
at Zone.scheduleMacroTask (http://localhost:8101/build/js/angular2-polyfills.js:283:40)
at http://localhost:8101/build/js/angular2-polyfills.js:133:26
at setTimeout (eval at createNamedFn (http://localhost:8101/build/js/angular2-polyfills.js:973:18), <anonymous>:3:37)
at process.nextTick (http://localhost:8101/build/js/app.bundle.js:64854:9)
at enqueuePromiseReactionJob (http://localhost:8101/build/js/app.bundle.js:37073:7)
at fulfillPromise (http://localhost:8101/build/js/app.bundle.js:37099:9)
------------- Elapsed: 52 ms; At: Sun Apr 17 2016 13:54:49 GMT-0400 (EDT) -------------
at Object.Zone.longStackTraceZoneSpec.onScheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:1354:23)
at ZoneDelegate.scheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:342:50)
at Zone.scheduleEventTask (http://localhost:8101/build/js/angular2-polyfills.js:286:40)
at zoneAwareAddEventListener (http://localhost:8101/build/js/angular2-polyfills.js:880:11)
at XMLHttpRequest.addEventListener (eval at createNamedFn (http://localhost:8101/build/js/angular2-polyfills.js:973:18), <anonymous>:3:43)
at XMLHttpRequest.desc.set [as onload] (http://localhost:8101/build/js/angular2-polyfills.js:776:19)
at XHRImpl.get (http://localhost:8101/build/js/app.bundle.js:30356:20)
at TemplateNormalizer.normalizeTemplate (http://localhost:8101/build/js/app.bundle.js:10294:30)
------------- Elapsed: 327 ms; At: Sun Apr 17 2016 13:54:49 GMT-0400 (EDT) -------------
at Object.Zone.longStackTraceZoneSpec.onScheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:1354:23)
at ZoneDelegate.scheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:342:50)
at Zone.scheduleMacroTask (http://localhost:8101/build/js/angular2-polyfills.js:283:40)
at http://localhost:8101/build/js/angular2-polyfills.js:133:26
at setTimeout (eval at createNamedFn (http://localhost:8101/build/js/angular2-polyfills.js:973:18), <anonymous>:3:37)
at process.nextTick (http://localhost:8101/build/js/app.bundle.js:64854:9)
at enqueuePromiseReactionJob (http://localhost:8101/build/js/app.bundle.js:37073:7)
at Promise.then (http://localhost:8101/build/js/app.bundle.js:37438:11)
Which Ionic Version? 2.x
Plunker that shows an example of your issue
http://plnkr.co/edit/fA0KB70ggJd0nptNvYpl?p=preview
Run ionic info from terminal/cmd prompt: (paste output below)
WARN: ionic.config.js has been deprecated, you can remove it.
Your system information:
Cordova CLI: 6.0.0
Gulp version: CLI version 3.9.0
Gulp local: Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.4
Ionic CLI Version: 2.0.0-beta.25
Ionic App Lib Version: 2.0.0-beta.15
ios-deploy version: 1.8.2
ios-sim version: 5.0.3
OS: Mac OS X El Capitan
Node Version: v0.12.7
Xcode version: Xcode 7.3 Build version 7D175
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 53 (18 by maintainers)
Commits related to this issue
- Issue #68: Creates Loading Feedback Creates LoadingStatusService. Updates pages to show loading mask when waiting on backend. Adds LoadingStatus class to hold loading and displayPromise. This is to ... — committed to mckoon/dose-amigos-ionic by mckoon 8 years ago
- Fixes #2 - Loader is recreated each time when it is needed. See https://github.com/driftyco/ionic/issues/6209 — committed to Hubesco/walkingdog-services by deleted user 8 years ago
- Fixes #2 - Loader is recreated each time when it is needed. See https://github.com/driftyco/ionic/issues/6209 — committed to Hubesco/walkingdog-services by deleted user 8 years ago
- Fixes #2 - Loader is recreated each time when it is needed. See https://github.com/driftyco/ionic/issues/6209 — committed to Hubesco/walkingdog-services by deleted user 8 years ago
- Fixes #2 - Loader is recreated each time when it is needed. See https://github.com/driftyco/ionic/issues/6209 — committed to Hubesco/walkingdog-services by deleted user 8 years ago
EDIT: Scratch that, turns out I needed/could use this._navController.present(loading).then( … ) to avoid dismissing before loading animation was done showing.
I’m getting the same error message, but in my case it happens if I do
loading.dismiss()too quickly after doingthis._navController.present(loading);My request to productService.findOne() might be cached and return immediately, and that’s when the exeption is thrown.
I am able to reproduce this in RC4.0
[What I Did] create a Loading as a share instance within my component and initialise the instance in constructor. So, various part of the component will call the .present() and .dismiss() of the same instance of Loading.
[How It Happen] -> call a method and present the Loading. -> method finish the task and dismiss the Loading. -> call another method and present the Loading. x-> throw error. Cannot read property ‘nativeElement’ of null
[How I Resolve] Do not initialise the Loading instance in constructor. Instead, initialise it every time before I .present() it.
Its not the best thing to do in term of programming practice. I hope the Ionic team will fix this.
@lucaspalencia I re-initialized the loading controller object everytime, we make a http.post and it worked for me. See if it works for you. loading = this.loadingCtrl.create({ content: ‘Loading’ }); init(){ this.loading = this.loadingCtrl.create({ content: ‘Loading’ }); }
And inside your http call, do this!
doHttpPost (){ this.init(); this.loading.present(); this.http.post(this.loginUrl, JSON.stringify(requestBody), {headers: headers}) .toPromise() .then(response => { this.auth.authLogin(type, userInfo.usuario, response.json().token); }) .catch(response => { this.loading.dismiss(); }); }
Cheers!
Updated to version 2.0.1 and still with the error, I have a method in my service with this code:
The first time that my http.post executed this.loading.dismiss(); everything works ok, but if i try again the console returns the error:
Some help?
What’s the current consensus on the presentation/dismissal of loading spinners? The rc2 docs and this thread don’t really match at all. Should we use NavController or LoadingController? Is it
dismiss()ordismissAll()? How should one prevent conflicts with other navigation events?I have a tabbed app, where each tab’s data provider wants to fire a loading spinner when needed. It works fine on the home/root tab, but tapping any other tab spits out a
Cannot read property 'nativeElement' of nullerror. I assume that means the nav action to the new tab hasn’t completed before the loading controller’spresent()method fires off, maybe? If so, how do I tap into that, given it’s an automagic event?Hello all! We are currently working on a refactor of the way “overlay components” like the loading component get pushed onto the stack. This should fix this issue. We may do a nightly that includes these changes soon and i will update this issue when that happens. Thanks!
I did many tests on the Loading Controller, and realized that to avoid this behavior, I had to create another instance of the object after call dismiss() on it.
I don’t know if it’s required, because the docs don’t explain that, but, to do the same tests that I did, follow this steps:
1 - Create a new object and assign this to a window property (previously defined on a typescript description file, otherwise it will not compile):
2- compile and load the app on browser.
3 - after that, it’s simple, you have to call, on the console of the browser:
window.testLoading.present()-> the Loading will show correctlywindow.testLoading.dismiss()-> the Loading will dismiss correctlyAfter that, if I call the
present()method onwindow.testLoading, I get the error:Cannot read property 'nativeElement' of null.Here is the problem. So, if I want to call the
present()method again on a Loading that I had called thedismiss()method before, I will get the error above.To call the
present()method, I had to first call the loadingCtrl to create a new instance on my window.testLoading property and only after that call thepresent()method.Maybe this information will help the devs to correct the error. 😄
I’m using events to control the behavior of Loading Control:
and I’m getting the following error:
loading-transitions.js:52 Uncaught TypeError: Cannot read property 'nativeElement' of nullthis is the full stacktrace, maybe it will help:
I tried to find a way to solve, but I was unable to solve it… ☹
My Ionic info:
Hello all, you all are correct, setting
dismissOnPageChangeto true makes this issue still reproducible, sorry about that! @tonyawad88 as a workaround you can use thedismiss()method on the loading component. So it would just be{{name of your loader}}.dismiss(). Hope that makes sense! I will look into this more.Hey all! Can you all try with this plunker? Looks like this issue is not reproducible anymore in our latest beta.7 release! http://plnkr.co/edit/IIfn2RKAMGheD8ISVr9A?p=preview I am going to close this for now, as it seems it was fixed. But if anyone can reproduce it again with beta.7 then i will reopen. Thanks for using Ionic everyone!
I have the same issue, though only when including the
dismissOnPageChange: trueparameter. (Does not throw an error if it’s set to false)