Bind new html to controller after calling trustAsHtml Angularjs

后端 未结 2 703
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 08:28

I\'m able to embed the new html once received from the server but later I need to bind it to the model. Even when I compile it 5 seconds after it\'s inserted and displayed the h

2条回答
  •  旧巷少年郎
    2021-01-23 09:08

    I was same problem and i make angular directive which will rerender html content. The code of directive is as follows.

    Directive:

    app.directive('bindUnsafeHtml', ['$compile', function ($compile) {
          return function(scope, element, attrs) {
                console.log("in directive");
              scope.$watch(
                function(scope) {
                  // watch the 'bindUnsafeHtml' expression for changes
                  return scope.$eval(attrs.bindUnsafeHtml);
                },
                function(value) {
                  // when the 'bindUnsafeHtml' expression changes
                  // assign it into the current DOM
                  element.html(value);
    
                  // compile the new DOM and link it to the current
                  // scope.
                  // NOTE: we only compile .childNodes so that
                  // we don't get into infinite loop compiling ourselves
                  $compile(element.contents())(scope);
                }
            );
        };
    

    }]);

    HTML:

    //your scope data variable which wil assign inn controller

    Dont forgot to import directive where you config your angularJs project.Hope this will help you.

提交回复
热议问题