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***');
3
  • the answer is no. if its in a template wont even run it. Commented May 12, 2014 at 14:00
  • @mpm so no way to conditionally call js files from template right? Commented May 12, 2014 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. Commented May 12, 2014 at 14:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
sure i agree just waiting for morep eople to answer or comment so that we can have more feedbacks about it
later i'll try with a directive cause i like this approach, if it works and no best alternatives i'll tell you thanks :)
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.

1 Comment

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 )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.