0

I am updating a DIV with the content of HTML page, that page has angular controller. when DIV is populated with the HTML, it does not BIND with Controller.

my index.html

<div id="mainDiv"> </div>

Content of home.html which I am loading with jQuery

<div ng-controller="BlogsController">
    {{Hello}}
</div>

And this is what I am calling on index.html

$().ready(function () {
    $("#mainDiv").load("pages/home.html");
})

Angular does not update the {{Hello}}, it seems like its not binding to loaded html via jQuery.

highwingers
  • 1,649
  • 4
  • 21
  • 39

2 Answers2

2

Angular is not aware of your changes in jQuery. You need to either load the html through angular and call the compile service:

$compile(HTML)($scope);

Or emit the event and tell angular to compile it. I just answered a question similar to this the other day on how to make angular aware of changes made through jquery: AngularJS legacy integration and content that contains asynchronously changing id doesn't trigger watcher

To sanitize you need to add the ngSanitize module to your project. But I believe you can just use the $sce service to tell angular not to bother sanitizing if you trust it

i.e.

<div id="mainDiv" compile-directive></div>

$().ready(function () {
    $.get('pages/home.html', function(data){
        $(document).trigger('customEvent', [data]);  
    });
});

angular.module('test').run(['$rootScope', function ($rootScope) {
    //capture jQuery events and re-broadcast them as angular events
    //events to capture
    var events = [
        'customEvent'
    ];

    //To Use: $scope.$on('eventName', function(ngEvent, jqEvent, data)
    $(document).on(events.join(' '), function(e) {
        $rootScope.$broadcast.apply($rootScope, [e.type].concat(Array.prototype.slice.call(arguments, 0)));   
    });
});

angular.module('test').directive('compileDirective', function($compile, $sce){
    return{
        restrict: 'AE',
        link: function(scope, element, attrs){
            scope.$on('customEvent', function(ngEvent, jqEvent, data){
                scope.$apply(function(){
                    angular.element.append($compile($sce.trustAsHtml(data))(scope));
                });
            };
         }
     };
 });
Community
  • 1
  • 1
Kevin F
  • 2,836
  • 2
  • 16
  • 21
  • 1
    I am doing this outside of angular JS, within a custom JS file. is it possible? – highwingers May 29 '15 at 23:30
  • 1
    Updated answer, should give you a good idea of how to transfer your data from jQuery to angular and compile it within angular – Kevin F May 29 '15 at 23:37
  • 1
    i am getting Argument 'fn' is not a function, got string – highwingers May 29 '15 at 23:54
  • 1
    What line gives you that error? I updated the ajax call to what the documentation suggests for getting html https://api.jquery.com/jquery.get/ – Kevin F May 29 '15 at 23:58
  • 1
    I got it to work, however its returning RAW HTML on page, can you tweak above code to allow unsafe HTML rendering? – highwingers May 30 '15 at 00:13
  • updated with $sce service. I don't have a ton of experience with this, so you might need to check a guide like this: http://odetocode.com/blogs/scott/archive/2014/09/10/a-journey-with-trusted-html-in-angularjs.aspx to get the last step figured out – Kevin F May 30 '15 at 00:20
0
  var htmlcontent = $('#loadhtml ');
    htmlcontent.load('/Pages/Common/index.html')
    $compile(htmlcontent.contents())($scope);
Parthipan S
  • 180
  • 9