*ngIf with providers updated from events


#1

I have a simple support page that shows the most recent unhandled exception, if there is one. In my custom ErrorHandler I publish an event, which then sets error values on a provider, which the view binds to via *ngif.

This whole thing works if the error is not on the support page, because the support view is re-loaded. If an error happens on the support page, the view never updates itself (until the next re-load). Any ideas?

View:

ion-card *ngIf="loggingService.lastUnhandledErrorMessage">
      <ion-card-header>
        <ion-icon name="alert"></ion-icon> Recent Error !
      </ion-card-header>

Error Handler / Service

this.events.subscribe(Constants.EVENT_ERROR_UNHANDLED, (error) =>{
  if(error){
    if(error.message){
      this.loggingService.lastUnhandledErrorMessage = error.message;
    }
  }
});

#2

How are you calling this subscribe method? View updates based on the lifecycle where this is called. Please share the complete code for the service. Also refer to the lifecycle events to place your call as needed.


#3

See my answer on the Scroll to Specific element thread.


#4

The event subscription happens on application startup. The event is firing, and the property on the service is updated. The ngIf just doesn’t pick it up unless I re-load the page.


#5

Are we absolutely certain that the view in question has the same instance of the logging service as the error handler that is setting the unhandled error message?

You could try changing your ngIf to bind to the result of a function hasError() in the view’s controller. In addition to perhaps solving the problem outright, that will allow you to set a breakpoint in that function and investigate.


#6

Same issue here. After subscribing an event, ngif does not update its condition.