在角度编译已经发生之后,在元素上编译角度

Jay*_*ley 0 angularjs angularjs-directive

我正在写一个指令,当点击它时,它从服务器加载一个html模板.我如何获得角度来编译它?

编辑:我应该提到模板用于加载到模态.

Jon*_*wny 8

你可以随时注入$ compile服务和$编译它.$compile('<p>{{total}}</p>')(scope)文档中的示例.

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

//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);
}
Run Code Online (Sandbox Code Playgroud)

此示例附加已编译的内容而不是替换它:

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