Angularjs 如何从控制器评估模板?

Angularjs 如何从控制器评估模板?,angularjs,Angularjs,我有一个模板: <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="{{p

我有一个模板:

<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>
生成如下内容:

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…

有人能解释一下如何实现我想要的吗?

如果您以错误的方式使用$compile函数,您应该通过传递
$scope
参数调用
$compile(html)
函数,如下所示

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.

好的,这似乎可以编译它,但是如何从中获取html片段而不是DOM对象呢?它还抛出了许多根无限摘要错误。@Chrillewoodz我可以知道你为什么需要它吗,因为让html参与其中并不能使角度绑定变得可行?尽管可以通过
compiledDOM.html()
compiledDOM[0].innerHTML来实现,因为我需要将代码段发送到后端,后端将存储它并将其作为静态html使用。但我不确定如何获取代码片段,因为正如您所说,它不会计算角度绑定。。hm.@Chrillewoodz no.。它将拥有由
ng repeat
呈现的所有记录,但一旦您从编译的DOM中提取
html
,您将获得该html&您将拥有原始DOM(例如,简单的html,即使ng click被绑定到它,也不会触发
ng click
事件),现在清楚了吗?但是我刚刚尝试了.innerhtml和html(),在对任何绑定进行评估之前,我只得到了原始html。但是我认为我已经找到了一个解决方案,它在将ad.html设置为已评估的html之前,等待摘要周期完成运行。所以你的解决方案是有效的。
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.