0

I am using jquery to append html in DOM. This html contains angular syntax which has controller and its variable binding. But once the html is appended, the variables initialized inside the controller does not populate their values instead it gets printed as {{ greeting }}.

JS:

$("#clickDiv").click(function(){
    var htmlStr = "<div ng-app='myapp'>"+
        "<div ng-controller='mainController'>"+
            "<div>{{ greeting.text }}</div>"+
            "<div>{{ val }}</div>"+
        "</div>"+
    "</div>";
    $("#right").html($compile(htmlStr));

});

var app = angular.module("myapp", []);
app.controller('mainController', ['$scope', '$compile', function($scope, $compile) {
    $scope.greeting = { text: 'Hello' };
    $scope.val = 123;
}]);

In the above code I am getting error - $compile is not defined.

JSFIDDLE: http://jsfiddle.net/ashwyn/7v6t6cqm/

1 Answer 1

1

For lazily loading angular app you should angular.bootstrap instead of ng-app

$("#clickDiv").click(function(){
    var htmlStr = "<div id='myApp'>"+
        "<div ng-controller='mainController'>"+
            "<div>{{ greeting.text }}</div>"+
            "<div>{{ val }}</div>"+
        "</div>"+
    "</div>";
    $('#right').html(htmlStr)
    angular.bootstrap($('#myApp'), ['myapp'])
});

Look at this answer too will give you brief idea about it.

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

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.