Angular-cache $.getScript JS code

716 Views Asked by At

I am using $.getScript to load some of my program code. My code looks something like this:

var mainJs = "/main.js";
$.getScript( mainJs )
    .then(function () {
        console.log("main.js loaded");
    });

The code works fine. However, I am thinking to use Angular-cache to do caching control on it. Is it possible to be done?

1

There are 1 best solutions below

5
Joy On

I am not sure whether angular is able to interfere the caching of jQuery. But by default $.getScript doesn't cache.

$.getScript is a shortcut of:

$.ajax({ 
    url: '',
    dataType: "script",
    cache: false
});

Reference: http://api.jquery.com/jQuery.getScript/

By default, $.getScript() sets the cache setting to false.

You need to use:

$.ajax({
    url: url,
    cache: true,
    dataType: 'script',
    ...
});