角度指令模板未解析HTML字符串

Angular directive template not parsing HTML string

本文关键字:HTML 字符串 指令      更新时间:2023-09-26

我有以下指令模板:

https://plnkr.co/edit/IXieg1mA0QDFIPmNd8El?p=preview

app.directive('myObject', function() {
  return{
    restrict: 'E',
    scope: {},
    controller: myControl,
    controllerAs: 'vm',
    bindToController: true,
    template: '<div>vm.name - {{vm.templateObject()}}</div>'
  }
  function myControl(){
    var vm = this;
    vm.name = 'John';
    vm.templateObject = function(){
      var username = '<em>john1234</em>';
      return username;
    }
  }
});

它应该输出指令的模板,templateObject函数返回<em>john1234</em>的HTML。但我需要将这个HTML与模板的其余部分一起解析。

如何在指令本身中解析此HTML?

使用ng-bind-html而不是常规{{expression}}