AngularJS + JQuery : How to get dynamic content working in angularjs

The question:

I am working on a Ajax app using both jQuery and AngularJS.

When I update content (which contains AngularJS bindings) of a div using jQuery’s html function, the AngularJS bindings doesn’t work.

Following is code of what I am trying to do:

$(document).ready(function() {
  $("#refreshButton").click(function() {
    $("#dynamicContent").html("<button ng-click='count = count + 1' ng-init='count=0'>Increment</button><span>count: {{count}} </span>")
</style><script src=""></script><style>.ng-invalid {
  border: 1px solid red;
<script src=""></script>
<script src=""></script>
<div ng-app="">
  <div id='dynamicContent'>
    <button ng-click="count = count + 1" ng-init="count=0">
    <span>count: {{count}} </span>

  <button id='refreshButton'>

I have dynamic content inside a div with the ID #dynamicContent, and I have a refresh button that would update contents of this div when refresh is clicked. Increment works as expected if I don’t refresh the content, but after I refresh, the AngularJS binding stops working.

This may not be valid in AngularJS, but I initially built application with jQuery and started using AngularJS later on so I can’t migrate everything to AngularJS. Any help with getting this working in AngularJS is greatly appreciated.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You need to call $compile on the HTML string before inserting it into the DOM so that angular gets a chance to perform the binding.

In your fiddle, it would look something like this.

    "<button ng-click='count = count + 1' ng-init='count=0'>Increment</button><span>count: {{count}} </span>"

Obviously, $compile must be injected into your controller for this to work.

Read more in the $compile documentation.

Method 2

Another Solution in Case You Don’t Have Control Over Dynamic Content

This works if you didn’t load your element through a directive (ie. like in the example in the commented jsfiddles).

Wrap up Your Content

Wrap your content in a div so that you can select it if you are using JQuery. You an also opt to use native javascript to get your element.

<div class="selector">
    <grid-filter columnname="LastNameFirstName" gridname="HomeGrid"></grid-filter>

Use Angular Injector

You can use the following code to get a reference to $compile if you don’t have one.

$(".selector").each(function () {
    var content = $(this);
    angular.element(document).injector().invoke(function($compile) {
        var scope = angular.element(content).scope();


The original post seemed to assume you had a $compile reference handy. It is obviously easy when you have the reference, but I didn’t so this was the answer for me.

One Caveat of the previous code

If you are using a bundle with minify scenario you will get in trouble when you deploy in release mode. The trouble comes in the form of Uncaught Error: [$injector:unpr] which is caused by the minifier messing with the angular javascript code.

Here is the way to remedy it:

Replace the prevous code snippet with the following overload.

    "$compile", function($compile) {
        var scope = angular.element(content).scope();

This caused a lot of grief for me before I pieced it together.

Method 3

Addition to @jwize’s answer

Because angular.element(document).injector() was giving error injector is not defined
So, I have created function that you can run after AJAX call or when DOM is changed using jQuery.

  function compileAngularElement( elSelector) {

        var elSelector = (typeof elSelector == 'string') ? elSelector : null ;  
            // The new element to be added
        if (elSelector != null ) {
            var $div = $( elSelector );

                // The parent of the new element
                var $target = $("[ng-app]");

              angular.element($target).injector().invoke(['$compile', function ($compile) {
                        var $scope = angular.element($target).scope();
                        // Finally, refresh the watch expressions in the new element


use it by passing just new element’s selector.
like this

compileAngularElement( '.user' ) ; 

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment