javascriptangularjs

Compile JS after HTML loaded


So I'm wanting to compile some JS code after the HTML is loaded.

var gifApp = angular.module('gifApp', []);

function MainCtrl ($scope) {
  $scope.push = function() {
    $("body").html("<div>{{ 1 + 1 }}</div>")
  }
}

Whenever the function push is called {{ 1 + 1 }} is displayed on the screen when I really want 2. Is it not possible to compile the code after the window is done loading? If it is possible, how do I fix this.


Solution

  • You are feeding the html() method a string, it is working correctly. I would change it to something like this:

    var sum = 1 + 1;
    $("body").html("<div>" + sum + "</div>");