Toastr allows you to globally define container, where toasts are displayed in this way:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule, ToastContainerModule } from 'ngx-toastr';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
ToastrModule.forRoot({ positionClass: 'inline' }), // <-- this
ToastContainerModule, // <-- lines
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
I want to achieve something similar, I want to place these toasts in a specific container but only within one component, so that it doesn't affect the rest of the toasts in the rest of the application. This is a pure component, no module or route.
Is something like this even possible?
What we did is to define a custom container on
AppComponent
in order to re-use it as needed from other components.So first you define a custom container in the template (pretty much like how it is explained in the docs). Beware that currently (2023-11-14) the docs are a bit outdated, instead of importing
ToastContainerModule
you now have to importToastContainerDirective
(which is now standalone) in either a standalone component or in the corresponding module (in our case inAppModule
):So in
AppModule
we have:And on the
app.component.html
template:The big difference to the docs, is that we don't add
{ positionClass: 'inline' }
as a "global" option on the module-import, but instead define different components and options for different use cases and leave the default as an overlay.Because we wanted to re-use the custom-inline container (and several others) in any component, we do all the configuration in a service e.g. CustomToastrService. Also, if you want to change the layout(s) you'll need custom component(s).
In the service you can define methods for different combinations of options/custom components, depending on your needs: