search

Home  >  Q&A  >  body text

angular.js - Angular instructions encapsulate third-party plug-ins, how to implement on-demand loading of third-party js

How to implement partial on-demand loading in

angularJS?


Well, the answer is ocLazyLoad. The following is part of the modified code.
ps: The principle is to use ajax to asynchronously request the target js file, and then put the requested text into the script tag in the callback function. Join the dom and then make plug-in related calls?

app.directive('mySelect2', function($timeout, $ocLazyLoad) {
  return {
    link: function(scope, ele, attr) {
      ele.hide();
      $ocLazyLoad.load(['https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
       'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css'])
       .then(function() {
            $(ele).select2(); 
        }); 
    }
  };
});
天蓬老师天蓬老师2755 days ago568

reply all(1)I'll reply

  • 为情所困

    为情所困2017-05-15 17:04:42

    Recommend ocLazyLoad, Chinese documentation, English documentation.

    reply
    0
  • Cancelreply