Skip to content
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

Explanation on how to register a TemplateRef in Global configuration #4318

Closed
lppedd opened this issue Oct 18, 2019 · 1 comment · Fixed by #4321
Closed

Explanation on how to register a TemplateRef in Global configuration #4318

lppedd opened this issue Oct 18, 2019 · 1 comment · Fixed by #4321
Labels

Comments

@lppedd
Copy link
Contributor

lppedd commented Oct 18, 2019

What problem does this feature solve?

Some components, like NzSpinComponent, allow specifying a TemplateRef<T> instance via the new global configuration.
However, for users it might not be clear how to programmatically create it in NgModule context (in fact it was a bit of a surprise to me too). The following is a snippet which can be integrated to show that.

app.module.ts

@Component({
  template: `
    <ng-template #nzIndicatorTpl>
      <span class="ant-spin-dot">
        <i nz-icon [nzType]="'loading'"></i>
      </span>
    </ng-template>
  `
})
export class SpinTplComponent {
  @ViewChild('nzIndicatorTpl', { static: true })
  nzIndicator!: TemplateRef<void>;
}

const nzConfigFactory = (
  injector: Injector,
  resolver: ComponentFactoryResolver
): NzConfig => {
  const factory = resolver.resolveComponentFactory(SpinTplComponent);
  const { nzIndicator } = factory.create(injector).instance;
  return {
    spin: {
      nzIndicator
    }
  };
};

@NgModule({
  declarations: [
    AppComponent,
    SpinTplComponent,
    ...
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    NgZorroAntdModule,
    ...
  ],
  providers: [
    {
      provide: NZ_CONFIG,
      useFactory: nzConfigFactory,
      deps: [Injector, ComponentFactoryResolver]
    },
    ...
  ],
  entryComponents: [SpinTplComponent, ...]
})
export class AppModule {}

Points to remember:

  • exporting the component is required for AOT compatibility
  • the component must be present in declarations and entryComponents

What does the proposed API look like?

Not needed.

@wzhudev
Copy link
Member

wzhudev commented Oct 19, 2019

This looks great. Could you please submit a PR to change the Global Configuration chapter of the official doc? You could make this approach more generic by renaming SpinTplComponent to GlobalTemplateComponent.

And, there's a simpler approach. In App component:

export class AppComponent implements OnInit {
 @ViewChild('nzIndicatorTpl', { static: true }) nzIndicator!: TemplateRef<void>;

  constructor(private nzConfigService: NzConfigService) {}

  ngOnInit(): void {
    this.nzConfigService.set('spin', { nzIndicator: this.nzIndicator });
  }
}

But this approach makes AppComponent bit of verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants