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="angular.js@1.2.x" 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/

Dave
  • 2,473
  • 2
  • 30
  • 55
  • 1
    you didn't add `ngRoute` as dependency, and you also didn't use `ngView` – MarkoCen Jul 01 '16 at 16:54
  • This post may help you, sounds like the same problem http://stackoverflow.com/a/16224671/1703519 – labago Jul 01 '16 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`](https://docs.angularjs.org/api/ngRoute/directive/ngView#!): "`$viewContentLoaded` Emitted every time the `ngView` content is reloaded." – Claies Jul 01 '16 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. – Dave Jul 01 '16 at 21:43
  • Try this. It seems like the same problem: http://stackoverflow.com/a/27197922 – Erőss János Oct 04 '16 at 08:37

0 Answers0