How to replace a html content with jsf reRender or ajax load, and rebind the new DOM with AngularJS?

若如初见. 提交于 2019-11-27 08:14:54

问题


Consider a bit of code:

<div id="dest">
  <p>Original Content</p>
  <p>Some data</p>
  <ul>
    <li ng-repeat="i in items">{{i.name}}</li>
  </ul>
</div>

or, using jsf, a panel:

<h:panelGroup id="dest">

Angularjs will understand the directive ng-repeat, and then, it will replaces the tag <li> by items list.

But, if your html has changed, by an ajax load, or by a jsf reRender, the new content doesn't will be recognized by AngularJS:

$('#dest').load('replace.html');

回答1:


We need to compile and apply the AngularJS scope for the new DOM:

On controller, create a function to compile a new html, and bind to same scope:

$scope.compileDOM = function($el) {
    ($compile($el))($scope);
    $scope.$apply();
};

And, after load, call it:

$('#dest').load('replace.html', function() {
    $dest.scope().compileDOM($dest);
});

Look to the function .scope(). It allows to find a correct AngularJS scope for a HTML element. It doesn't need to point directly to element with ng-controller, any element under the ng-controller can be used.

Plunker sample: PLUNKER



来源:https://stackoverflow.com/questions/27131078/how-to-replace-a-html-content-with-jsf-rerender-or-ajax-load-and-rebind-the-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!