我有这个模板:
<div class="container-fluid">
<div class="row">
<div class="col-xs-12" ng-repeat="product in ad.products">
<a href="{{product.link}}">
<h1>{{product.title}}</h1>
<img src="{{product.src}}">
<p>{{product.description}}</p>
<h5>{{product.price}}</h5>
</a>
</div>
</div>
</div>
在我的控制器中,我需要评估此模板,以便检查已选择的产品数量,然后将每个产品的值插入到模板中。完成之后,我还需要删除ng-repeat,这样它就不会在外部页面中触发错误,如果不存在angular,则会使用此错误。但是,我想我只是使用正则表达式来查找表达式中的ng-repeat
和所有内容,然后将其删除。
我一直在关注$interpolate
和$compile
,但我无法弄清楚如何从我的控制器中使用它们以便它能够满足我的需求。这是因为当我在我的模板上使用这些,然后控制台记录模板值时,它就是一个带有大量废话的函数。
这样做:
ad.html = $compile(res.data, $scope);
生成如下内容:
function(b,c,d){rb(b,"scope");e&&e.needsNewScope&&(b=b.$parent.$new());d=d||{};var h=d.parentBoundTranscludeFn,k=d.transcludeControllers;d=d.futureParentElement;h&&h.$$boundTransclude&&(h=h.$$boundTr…
有人能说明如何实现我的目标吗?
答案 0 :(得分:1)
您正在以错误的方式使用$ compile函数,您应该通过传递$compile(html)
参数来调用$scope
函数。
var compiledDOM = $compile(res.data)($scope);//then do append this DOM to wherever you want
ad.html = compiledDOM.html(); //but this HTML would not make angular binding working.