Skip to content

ngOnDestroy not called for injectables created via factory providers #22466

Description

@IgorMinar

Current behavior

export class DestroyTest implements OnDestroy {
  ngOnDestroy() {  // <<<<< this method is NEVER called
    console.log('ngOnDestroy DestroyTest called');
  }
}

export class DestroyTest2 implements OnDestroy {
  ngOnDestroy() { // <<<<< this method IS called when the app ref is being destroyed
    console.log('ngOnDestroy DestroyTest2 called');
  }
}

export function createDestroyTest() {
  return new DestroyTest();
}

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, HelloComponent ],
  bootstrap:    [ AppComponent ],
  providers: [
    {provide: DestroyTest, useFactory: createDestroyTest},
    DestroyTest2
  ]
})
export class AppModule {
  constructor(
    destroyTest1: DestroyTest, destroyTest2: DestroyTest2) {}
}

Expected behavior

the ngOnDestroy method, if present, should be called on all values provided via DI regardless of the provider type.

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-h15slp

Environment


Angular version: 6.0.0-beta.x

// Note: reported by GX

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions