如何让我重新评估angular.js /重新编译内部HTML? [英] How do I make angular.js reevaluate / recompile inner html?

查看:334
本文介绍了如何让我重新评估angular.js /重新编译内部HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了修改它的内在HTML指令。 code迄今:

I'm making a directive that modifies it's inner html. Code so far:

.directive('autotranslate', function($interpolate) {
    return function(scope, element, attr) {
      var html = element.html();
      debugger;
      html = html.replace(/\[\[(\w+)\]\]/g, function(_, text) {
        return '<span translate="' + text + '"></span>';
      });
      element.html(html);
    }
  })

它的工作原理,不同之处在于内层的HTML不是由角进行评价。我想触发元素的子树升值。有没有办法做到这一点?

It works, except that the inner html is not evaluated by angular. I want to trigger a revaluation of element's subtree. Is there a way to do that?

感谢:)

推荐答案

您必须 $编译你的内心像HTML

You have to $compile your inner html like

.directive('autotranslate', function($interpolate, $compile) {
    return function(scope, element, attr) {
      var html = element.html();
      debugger;
      html = html.replace(/\[\[(\w+)\]\]/g, function(_, text) {
        return '<span translate="' + text + '"></span>';
      });
      element.html(html);
      $compile(element.contents())(scope); //<---- recompilation 
    }
  })

这篇关于如何让我重新评估angular.js /重新编译内部HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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