0

I am using ChartJS to create a chart on a page in angular. I am running into the issue of when I navigate to a new page, and back to the original page, the JS is not called again.

Is there a way to call a javascript function or file every time an angular page navigates? I guess I'd then just see if my selector exists on the page and then call the function.

$(document).ready(function () {
    //Call on every page:
    (function ($) {
        $(window).load(function () {
            $(".bubbleScrollbar").mCustomScrollbar({
                theme: "rounded-dots"
            });

            $(".hBubbleScrollbar").mCustomScrollbar({
                //theme: "minimal-dark",
                theme: "rounded-dots-dark",
                axis: "x",
                advanced: {autoExpandHorizontalScroll: true},
                mouseWheelPixels: 150

            });

        });
    })(jQuery);

// on all chart pages:


    var ctx = $('#chart-TicketsResolved').get(0).getContext("2d");
    var data = [
        {
            value: 300,
            color: "#50AD7E",
            label: "Resolved"
        },
        {
            value: 200,
            color: "#d9d9d9",
            label: "Open"
        }
    ];
    var myDoughnutChart = new Chart(ctx).Gauge(data);
});
Jeff
  • 4,285
  • 15
  • 63
  • 115

1 Answers1

1

You can call your function by listenning to window.hashchanged event.

window.onhashchange = function () {
  console.log('my function runs every time the # hash changes');
}

See more here: How to detect URL change in JavaScript

Community
  • 1
  • 1
AlexStack
  • 16,766
  • 21
  • 72
  • 104