ng-zorro-antd
ng-zorro-antd copied to clipboard
[Notification] Support for component as parameter
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.
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.
No solution for now unfortunately, this is something I kinda need to PR myself at this point 😄
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
});
}
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 !