0

How can i include multiple js files in lazy load, so when page finished to load and angular has been executed?

I tryed with a custom directive and the code looks like: //html

<ng-lazy-load data-type="script" data-src='["file.js"]'></ng-lazy-load>

//directive

.directive('ngLazyLoad', [function () {
    return {
      restrict: 'E',
      templateUrl:'views/modules/module_lazy_load.html',
      scope:true,
      link: function ($scope, element, attr) {
        if(attr.type === 'script'){
          $scope.src = JSON.parse(attr.src);
        }

      }
    };
  }]);

//directive view

<script ng-if="src" ng-repeat="path in src" src="{{path}}"></script>

//file.js to be included in lazy load

alert('daf***');
  • the answer is no. if its in a template wont even run it. – mpm May 12 '14 at 14:00
  • @mpm so no way to conditionally call js files from template right? – itsme May 12 '14 at 14:00
  • there is always a way it is just javascript after all.the question is ,does it make sense to do that. But you can still append a script to the body element in pure javascript. – mpm May 12 '14 at 14:06
1

I dont think so, as script is loaded before angular can take over.

How about a directive that can check your stuff and then you can $document.write the script to the index.html?

Have the controller in charge of the scope variable be injected into the directive, then it can write to the html conditionally.

|improve this answer|||||
  • 1
    we had some dirty nasty code that did this in a disgusting JS file, but if its included in a directive then why not - as its localized to one area and scope. – Sten Muchow May 12 '14 at 14:03
  • sure i agree just waiting for morep eople to answer or comment so that we can have more feedbacks about it – itsme May 12 '14 at 14:04
  • later i'll try with a directive cause i like this approach, if it works and no best alternatives i'll tell you thanks :) – itsme May 12 '14 at 14:10
0

Nay. It's a temporally inconsistent idea: A script tag is executed when it's encountered while parsing the DOM. What would it even mean to "switch it off" later? It will have already been executed by then.

|improve this answer|||||
  • if you create a new DOM element when page is ready, i guess it get parsed and executed also if its <script> tag nope? i don't need to switch off just to switch it on when i want ( if possible ) – itsme May 12 '14 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.