ng-zorro-antd icon indicating copy to clipboard operation
ng-zorro-antd copied to clipboard

[Notification] Support for component as parameter

Open Supamiu opened this issue 5 years ago • 6 comments

What problem does this feature solve?

When creating a notification from a service that has no access to a template ref (like in a notification service that's started with APP_INITIALIZER), not having the option to create a custom notification is blocking some reusability. This could also be better for overall reusability accross the code base, when showing complex notifications from services that are not called from a component directly.

What does the proposed API look like?

Kind of what the NzModalService.create method offers, being able to provide a component as content, and then params using a Partial<T> where T is the type of the component.

Supamiu avatar Nov 30 '20 17:11 Supamiu

Wondering if you've found a workaround for this?

I'm in an identical situation - wanting to use a service to generate notifications using a TemplateRef<{}>, however, it seems like I can't create a TemplateRef from within a service.

crossan007 avatar Aug 15 '22 15:08 crossan007

No solution for now unfortunately, this is something I kinda need to PR myself at this point 😄

Supamiu avatar Aug 15 '22 16:08 Supamiu

I ended up creating a proxy component here which allowed me to define the TemplateRef and invoke NzNotificationService

notification-service.ts exposes a function to obtain a handle on the app container and instantiate my proxy component

public setContainer(container: ViewContainerRef) {

    this.container = container;

    log.debug("Set container to ", this.container)

    const c = this.container.createComponent(UserNotificationComponent)

    this.notificationComponentInstance = c.instance;

  }

App.component.ts calls this function during construction


constructor(
 @Inject(ViewContainerRef) viewContainerRef
) {


this.userNotifications.setContainer(viewContainerRef);
...

And then my component exposes a function that calls it's NzNotificationService and passes the template defined in the component's HTML

@ViewChild('tplNewMessage') tplNewMessage: TemplateRef<{}>;


public showMessage(msg: FrontendUserNotification){

    this.notify.template(this.tplNewMessage, {

      nzData: msg,

      nzDuration: 5000

    });

  }

crossan007 avatar Aug 15 '22 19:08 crossan007

Is this going to be supported ? Material supports both openFromComponent and openFromTemplate https://material.angular.io/components/snack-bar/api Would be great to have this !

mehdigmira avatar May 02 '23 14:05 mehdigmira