Angular 2 doesn't update view after exception is thrown

后端 未结 2 1052
遇见更好的自我
遇见更好的自我 2020-12-16 17:49

When an exception is caught by Angular 2\'s exception handler, the UI no longer \'updates\'.

I have a very simple example here:

相关标签:
2条回答
  • 2020-12-16 18:13

    Don't rely on code execution after an unhandled Exception happened. You have to handle the exception in the place where you expect it to happen.

    Error handling: http://www.javascriptkit.com/javatutors/trycatch.shtml

    0 讨论(0)
  • 2020-12-16 18:27

    Since angular 4.1.1 (2017-05-04) https://github.com/angular/angular/commit/07cef36

    fix(core): don’t stop change detection because of errors

    • prevents unsubscribing from the zone on error
    • prevents unsubscribing from directive EventEmitters on error
    • prevents detaching views in dev mode if there on error
    • ensures that ngOnInit is only called 1x (also in prod mode)

    it should work without additional code

    @Component({
      selector: 'my-app',
      template : `
        {{aValue}}
        <button (click)='doIt()'>do it</button>
        <button (click)='breakIt()'>break it</button>
      `
    })
    
    export class App implements OnInit {
      private aValue: boolean = true
    
      doIt(){
        console.log('Doing It')
        this.aValue = !this.aValue
      }
    
      breakIt(){
        console.log('Breaking It')
        throw new Error('some error')
      }
    }
    

    Plunker Example

    0 讨论(0)
提交回复
热议问题