编译元素后角角汇编已经发生了 [英] compile angular on an element after angular compilation has already happened

查看:131
本文介绍了编译元素后角角汇编已经发生了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个指令,并点击时加载从服务器HTML模板。我怎样才能得到角度来编译呢?

I'm writing a directive and when clicked it loads a html template from the server. How can I get angular to compile this?

编辑:我也许应该提及模板用于装入一个模式

I should probably mention the template is used for loading into a modal.

推荐答案

您可以注入$编译服务和​​$只要你想编译它。 $编译('< P> {{总}}< / P>')(范围)距离的docs 的。

You can inject the $compile service and $compile it whenever you want. $compile('<p>{{total}}</p>')(scope) is the example from the docs.

在实践中,你可能会想要做这样的事情:

In practice you'll probably want to do something like this:

//Example as a directive's link function
function link(scope, element, attributes){
  scope.name = "world";
  template = "<p>hello {{name}}</p>"; //this could come from anywhere
  element.html(template);
  $compile(element.contents())(scope);
}

这个例子追加编译内容而不是替换它的:

This example appends the compiled contents instead of replacing it:

function link(scope, element, attributes){
  scope.something = "this is bananas";
  $compile("<p>{{something}}</p>")(scope, function(cloned, scope){
    element.append(cloned);
  });
}

这篇关于编译元素后角角汇编已经发生了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆