How can I change the position of a specific toast per use case using ngx-toastr in Angular 5

15,487

Solution 1

The 3rd parameter of the error method is used to provide the position of the toastr message (among other things).

this.toastrService.error('There was an error loading the Asset List!', 'Asset Register');

this.toastrService.warning('Some warning message', 'some title', {
   positionClass: 'toast-bottom-right' 
});

Solution 2

Add this in style.css

.toast-top-center {
  bottom: 0;
  margin: 0 auto;
  right: 0;
  left: 0;
  width: 100%;
}

Insert this in your toast function

show(config: { type: string, title: string, subTitle?: string }): void {
  switch (config.type) {
        case 'Success':
          this._toastr.success(config.subTitle ? config.subTitle : '', config.title,{ positionClass: 'toast-top-center'});
          break;
        case 'Error':
          this._toastr.error(config.subTitle ? config.subTitle : '', config.title, { positionClass: 'toast-top-center'});
          break;
        case 'Warning':
          this._toastr.warning(config.subTitle ? config.subTitle : '', config.title, { positionClass: 'toast-top-center'});
          break;
        case 'Info':
          this._toastr.info(config.subTitle ? config.subTitle : '', config.title, { positionClass: 'toast-top-center'});
          break;
        default:
          break;
      }
}

Share:
15,487
onmyway
Author by

onmyway

BY DAY: Geek. I am part of a dynamic development team, developing specialized media monitoring software. As part of the team, my current focus is developing and enhancing our front-end client dashboards. The aim is to create professional, analytical portal dashboards, visualizing media coverage and trends in advertising, and social media. I am using AngularJs, JavaScript, HTML, CSS, HighCharts.js and other JavaScript plugins and scripts for the dashboards. I also make use of task-based tools such as Grunt and Gulp, and CSS, HTML and JavaScript minifiers. As part of future growth and projects, I am busy brushing up and Angular 2 - using Typescript - and Ruby and Ruby on Rails for future projects. In addition to this, I keep up with C# and .Net trends, and actively develop my skill set in this regard. BY NIGHT: Husband, father, gamer, and more!

Updated on June 13, 2022

Comments

  • onmyway
    onmyway almost 2 years

    I have been reading up on this on the actual site for ngx-toastr ngx-toastr, and other posts on Stack Overflow, but cannot find a clear solution for my work case.

    I am trying to change the position of the toastr for specific use cases. Example; when it is an error, show the toastr on the top.

    I have a very vanilla setup.

    In my app.module.ts I have the following:

    import { ToastrModule } from 'ngx-toastr';
    

    In my imports of app.module.ts I have:

    imports: [
        BrowserModule,
        ToastrModule.forRoot({
          timeOut: 3500,
          positionClass: 'toast-bottom-center',
          preventDuplicates: true,
    }),
    

    In my components I declare the toastr in my constructor:

    constructor(private toastr: ToastrService) {}
    

    And I use the toastr as follows:

    this.toastr.error('There was an error loading the Asset List!', 'Asset Register');
    

    As per my setup, all toast show in 'toast-bottom-center'. How can I modify this call to show the toast on the top?

    this.toastr.error('There was an error loading the Asset List!', 'Asset Register');