使用AngularJS解析HTML片段 [英] Parse HTML fragment with AngularJS
问题描述
我有一部分屏幕是通过AJAX动态检索的,并替换了现有的部分(考虑一个分页的网格,当您单击下一步"时,将得到一个新的HTML表,而不是替换当前的HTML表).该片段可能包含AngularJS绑定,例如一些需要附加的指令或次要数据绑定.
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.
是否有一种方法可以使AngularJS解析新片段而无需重新解析整个文档?
Is there a way to make AngularJS parse that new fragment without reparse the whole document?
推荐答案
您可以使用 $ compile
例如您有
you can use $compile
for example you have
var htmlText = "<div>{{name}} <select>...</select></div>";
您可以在指令中这样做
$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);
这篇关于使用AngularJS解析HTML片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!