Show toastr notifications after action in JS file

205 views Asked by At

I'm new in Angular 5, and I'm implementing notifications, they work correctly if I import service to typescript component like:

import { ToastrService } from 'ngx-toastr';

 constructor(
    private toastr: ToastrService
  ) { }

   //call method
 this.toastr.success('Alert works')

This method works correctly, my question is, how can I use this into normal javascript file where I have Ajax call like:

jQuery(document).ready(function() {
  DataTablePerfiles.init();
  $(".myToggler").on("switchChange.bootstrapSwitch", togglerFunction);
});

function togglerFunction(event, state) {
  var data = {
    UsuarioId: $(this)
      .attr("data-id")
      .toString(),
    Activo: event.target.checked
  };

$.ajax({
    method: "PUT",
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer " + token
    },
    url: "http://myapi/api/users/activate",
    data: JSON.stringify(data),
    success: function(data) {
       //this.toastr.success("changed correctly");
    },
    error: function(e) {
       //this.toastr.warning(e._body);
    }
  });

Problem is I can't import toastr service as same as typescript into js file, how can I import it or how can I do in typescript to listen ajax function then display alerts? Regards

0

There are 0 answers