compile angular on an element after angular compilation has already happened

不想你离开。 提交于 2019-12-03 09:14:28

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);
  });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!