Parse HTML fragment with AngularJS

混江龙づ霸主 提交于 2019-12-24 10:43:17

问题


I have a part of a screen that is retrieved dynamically through AJAX, and replaces an existing part (think about a paginated grid, that when you click on "next" you get a new HTML table than replaces the current). That fragment may content AngularJS bindings, like some directives that needs to be attached or minor data bindings.

Is there a way to make AngularJS parse that new fragment without reparse the whole document?


回答1:


you can use $compile
for example you have

var htmlText = "<div>{{name}} <select>...</select></div>";

in your directive you can do like this

$scope.compiled= $compile(htmlText)($scope);

To parse a replaced section of the document would be:

var el = angular.element(document.getElementById('#container'));
el.html(ajaxHtml);
$compile(el.contents())(scope);


来源:https://stackoverflow.com/questions/28170247/parse-html-fragment-with-angularjs

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