0

I have an AngularJS app where I got some data from a webservice and parse some HTML to the template with ng-bind-html ... but when I try to bind data inside the ng-bind-html - nothing happens .. anyone?

I have a little example here,.. not the right case.

HTML

<div ng-controller="MyCtrl">
    <div ng-bind-html="post"></div>
</div>

Javascript

angular.module('myApp',[])
.controller('MyCtrl', function($scope, $sce) {
    $scope.name = 'World';
    $scope.post = $sce.trustAsHtml("<h1>hello {{name}}</h1>");
});

http://jsfiddle.net/bugd67e3/

6
  • 1
    Is the HTML fragment in your example coming from the webservice? Commented Aug 20, 2014 at 13:58
  • Yep ... encoded as json .. Commented Aug 20, 2014 at 13:58
  • Have you specified the ng-app? Commented Aug 20, 2014 at 13:59
  • Yep.. everything else works just fine ;) Commented Aug 20, 2014 at 14:04
  • Do you have sanitize included in you app? also see github.com/angular/angular.js/issues/4992 Commented Aug 20, 2014 at 14:19

2 Answers 2

3

DEMO

Add this directive

angular.module("myApp").directive('compileTemplate', ["$compile", "$parse", function($compile, $parse) {
    return {
        restrict: 'A',
        link: function($scope, element, attr) {
            var parse = $parse(attr.ngBindHtml);
            function value() { return (parse($scope) || '').toString(); }

            $scope.$watch(value, function() {
                $compile(element, null, -9999)($scope); 
            });
        }
    }
}]);    
Sign up to request clarification or add additional context in comments.

1 Comment

Your demo does not use 2-way binding Could you add an ng-model to a form element and demo 2-way binding please?
0

In my case,I have $scope.content which is dynamic pulled from back-end, with delay of 1-2 seconds. CKEDITOR.inline() doesn't work if the content is populated after CKEDITOR is already initialized. I ended up with the following solution, which tackles the 2ways data binding and loading delay well.

<div id="divContent" contenteditable="true" ck-editor ng-model="Content">
</div>


angular.module('ui.ckeditor', []).directive('ckEditor', ["$compile", "$parse", "$sce", function ($compile, $parse, $sce) {
    return {
        require: '?ngModel',
        link: function (scope, elm, attr, ngModel) {
            var hasInit = false;
            scope.$watch(attr.ngModel, function (newValue, oldValue, scope) {
                if (newValue && !hasInit) {
                    hasInit = true;
                    var content = $.parseHTML(newValue.toString());
                    $(elm[0]).append(content);
                    CKEDITOR.inline(elm[0]);
                    elm.on('blur keyup change', function () {
                        scope.$evalAsync(read);
                    });
                }
            })

            // Write data to the model
            function read() {
                var html = elm.html();
                ngModel.$setViewValue($sce.trustAsHtml(html));
            }
        }
    };
}]);

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.