1

I am trying to get $viewContentLoaded to work, but no matter what I try, it just refuses to fire. Here is where I am currently.

Javascript (directive.js)

var app = angular.module('myapp', []);
app.controller('MainCtrl', function($scope, $timeout) {

$scope.$on('$viewContentLoaded', function () {
    $timeout(function () {
        var x = document.getElementById("iframe");
        alert(x); //Never Fires.
    }, 0);
  });
});

The HTML

<!DOCTYPE html>
<html ng-app="myapp">

<head>
<meta charset="utf-8" />

   <script data-require="[email protected]" src="http://code.angularjs.org/1.2.7/angular.js" data-semver="1.2.7"></script>
   <script src="script/directive.js"></script>
</head>

   <body ng-controller="MainCtrl">
       <iframe id="iframe"></iframe>
   </body>

</html>

I have tried this multiple ways:

I have taken the $timeout stuff out. I've tried using it combined with routing. I've tried using $watch in place of $on. This was able to give me an alert, but not at the correct time. I've tried this in IE11 and Chrome, neither work There is nothing logged in the console.

This just flat out will not work. Where am I going wrong?

Edit: https://jsfiddle.net/U3pVM/25952/

5
  • 1
    you didn't add ngRoute as dependency, and you also didn't use ngView Commented Jul 1, 2016 at 16:54
  • This post may help you, sounds like the same problem stackoverflow.com/a/16224671/1703519 Commented Jul 1, 2016 at 16:55
  • as @MarkoCen said, you aren't using ngRoute and don't have an ng-view, so nothing ever fires the event you are listening for. Documentation on ng-view: "$viewContentLoaded Emitted every time the ngView content is reloaded." Commented Jul 1, 2016 at 17:23
  • Thanks guys! You all rock! I didn't realize $viewContentLoaded was only supposed to work in combination with ngView and ngRoute. I've set up another page using routing and things are working great now. Commented Jul 1, 2016 at 21:43
  • Try this. It seems like the same problem: stackoverflow.com/a/27197922 Commented Oct 4, 2016 at 8:37

0

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.