3

I'm writing a directive for creating text from templates, but I cannot get to render my final result into HTML. This is the directive:

.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);
        }
      });
    }
  }
});

The data comes from the controller and once the user has chosen between some options, hence the $watch.

Thank you!

1 Answer 1

2

You should use ng-bind-html to bind html to the div

template: "<div id='template' ng-bind-html='dc.finalValue'></div>",
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thanks! I had to add as well the 'ngSanitize' to the module and the $sce.trustAsHtml(value) for having accepted the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.