0

In my Web App, I have to use ng-include to dynamically load content. However, this content also contains scripts (preferably even additional Controllers), which ng-include doesnt load.

None of the approaches I found are really satisfying.

I thought perhaps something like

<div>contentUrl.onChanged{ load HTML and load/execute scripts}</div> 

to replace

<div ng-include="contentUrl"></div>

However, I couldn't find anything and my JavaScript-knowledge is not yet sufficient to do this myself.

Ajean
  • 5,528
  • 14
  • 46
  • 69
Claas M.
  • 267
  • 3
  • 11
  • Haha I read through about 20 Questions about this before I found the Question you just posted. By then I was too frustrated to scroll further than the accepted answer - turns out the solution was just beneath that. Now I feel stupid. Thanks :) – Claas M. Aug 14 '15 at 16:16

1 Answers1

0

The answer can be found here. Pay Attention, its not the accepted answer ;) Thanks @James Bierley

Here's a quick fix created by endorama:

/*global angular */ (function (ng) { 'use strict';

var app = ng.module('ngLoadScript', []);

app.directive('script', function() { return { restrict: 'E', scope: false, link: function(scope, elem, attr) { if (attr.type === 'text/javascript-lazy') { var code = elem.text(); var f = new Function(code); f(); } } }; });

}(angular));

Simply add this file, load ngLoadScript module as application dependency and use

type="text/javascript-lazy" as type for script you which to load lazily in >partials:

console.log("It works!");
Community
  • 1
  • 1
Claas M.
  • 267
  • 3
  • 11
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Jost Aug 14 '15 at 17:29
  • Changed that. The Quote function is a pain in the backside -.- – Claas M. Aug 14 '15 at 18:54