I've searched hi and lo and can't find a post that is relevant/helpful for my particular situation.
I have an app that loads various widgets, some of which load JSONP that contains 3rd party JS that needs to be executed.
I've been able to get all the way to loading the JS in the widget element (directive template), however, I can't figure out how to get that JS to actually execute.
I've thought about maybe doing this in compile, but am new to that so not sure if it's the right approach. I tried using eval() but that didn't work. I tried .$digest and .$apply but that didn't work either. I just need to get the JS to execute as if I put it in the HTML without any Angular. When I do it via a basic jquery/HTML page, it works as expected.
Here is the relevant code:
angular.module('protoApp')
.directive('thirdPartyWidget',['$http',function($http){
return {
restrict: 'E',
scope: {},
template: '<div id="myWidget"></div>',
replace: true,
link: function($scope, $el, $attrs){
$http({
method: 'JSONP',
responseType: 'jsonp',
url: $scope.url,
data: thirdPartyWidgetProperties
})
.then(function(response){
// this will properly insert JS into my $el
// but this JS will not do anything/run.
$el.append(response.data.html);
});
}
}
}]);
(I'm aware that I should be using a service instead of $http directly here, but I'm just trying to get this to work as simply as possible.)
As an example, one of the JS scripts is for Google Analytics. I'm expecting that after the JSONP is loaded into the $el, there should be a request to GA, just as it does in the non-AngularJS version.
When I inspect the DOM, I can see the script is indeed being appended into the $el, but it doesn't run or do what it should. It's just there like this:
<div class="ng-isolate-scope"><div id="remoteProfiles"></div><script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-99999-99', 'website_name.com');
ga('send', 'pageview');
</script>
<script type="text/javascript">...3rd party widget stuff...</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.1/require.min.js"></script>
<!--<script src="//requirejs.org/docs/release/2.1.11/comments/require.js"></script>--></div>
So, see, it's there. How do I get it to actually run?
Any info, suggestions, posts, anything you can point me to would be appreciated.