Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run script after view is loaded AngularJS

Im new at AngularJS, and i have this problem;

I fill part of my view with html from a variable in my controller:

<div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div>

Everything goes fine until i decide to run a script. The script is shown, but it doesn't run. I think the problem is the view is filled with my variable controllers after loading the page, so the script doesn't run. The reason that i am using a variable in my controller to storage the script is because i will have to get the script from somewhere else, and its frequently changed.

Is this a viable way to run my script?.

Here is an example of my code:

View:

    <div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div>

Controller:

    .controller('browseCtrl', function($scope,$sce) {

      $scope.video = '<div id="idxx1" style="width: 460px; height: 290px;" itemprop="video" itemscope itemtype="http://schema.org/VideoObject"></div><script>addeere3ejs("idxx1",  "172653", "24431581", "1_fq2w6oc2");</script>';

      $scope.deliberatelyTrustDangerousSnippet = function() {
      return $sce.trustAsHtml($scope.video);
    };
})

If my question it is unclear i would try to explain it better.

like image 411
Nicolas Del Valle Avatar asked Oct 20 '22 07:10

Nicolas Del Valle


1 Answers

You can use the viewContentLoaded event in your controller.

$scope.$on('$viewContentLoaded', function(){
 // Run after view loaded.
});

Not exactly sure if ng-bind-html will allow a script to be ran like that. You may need to wrap it in angulars brackets to auto-run on load.

 <div ng-bind-html="{{deliberatelyTrustDangerousSnippet()}}"></div>
like image 120
Christopher Marshall Avatar answered Oct 23 '22 20:10

Christopher Marshall