【问题标题】:AngularJS: render HTML in directive controllerAngularJS:在指令控制器中呈现 HTML
【发布时间】:2015-10-20 09:35:50
【问题描述】:

我正在编写一个从模板创建文本的指令,但我无法将最终结果呈现为 HTML。这是指令:

.directive('description', function($timeout){
  function descriptionCtrl(){
    var self = this;
    self.result = "";
    self.init = function(value) {
      console.log("template in directive",value);
      self.finalValue = "<div>HI <input type='text' id='hi' /></div>";
    };
  }
  return {
    restrict: 'AE',
    controller : descriptionCtrl,
    controllerAs: 'dc',
    scope: {
      text: "="
    },
    replace: true,
    template: "<div id='template'>{{dc.finalValue}}</div>",
    link: function(scope, iElement, iAttrs, ctrl) {
      scope.$watch("text", function(value){
        if(value!==undefined){
          $timeout(ctrl.init(value),0);
        }
      });
    }
  }
});

数据来自控制器,一旦用户在一些选项之间进行了选择,因此 $watch。

谢谢!

【问题讨论】:

    标签: javascript html angularjs angularjs-directive controller


    【解决方案1】:

    您应该使用ng-bind-html 将html 绑定到div

    template: "<div id='template' ng-bind-html='dc.finalValue'></div>",
    

    【讨论】:

    • 完美,谢谢!我还必须将“ngSanitize”添加到模块和 $sce.trustAsHtml(value) 以接受输入。
    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2013-03-18
    • 1970-01-01
    • 2015-09-24
    相关资源
    最近更新 更多