0

I need to render the $scope.htmlView tags in to html view.

I already tried using ng-bind-html. It renders the html tags but scope variable values will not appear.

How can I render both html tags and and scope variable values?

This is the controller:

$scope.newObj = {
  billStatus : true;
  eventTime : "2015-01-10"
};

$scope.htmlView = '<p>{{newObj.eventTime}}</p>    <div style="margin-top: -15px;"><md-checkbox ng-checked="{{newObj.billStatus}}" style="margin-left: 0px;" aria-label="Bilable"><span style="margin-left:0px;">Bilable</span> </md-checkbox></div>'

Expected result is:

<p> 2015-01-10</p> 
<div style="margin-top: -15px;">
  <md-checkbox ng-checked="true" style="margin-left: 0px;" aria- label="Bilable">
    <span style="margin-left:0px;">Bilable</span>
  </md-checkbox>
</div>

I search over the internet over days and still could't find out a way to figure out this. please help me. thank you.

4
  • Did you try doing this $sce.trustAsHtml(string) trustAsHtml? Commented Jun 23, 2016 at 7:15
  • 1
    Check this question stackoverflow.com/questions/17417607/… Commented Jun 23, 2016 at 7:19
  • Reference here (stackoverflow.com/questions/18157305/…) Commented Jun 23, 2016 at 7:33
  • Check updated answer. If you need variables, you have to use $compile to evaluate them Commented Jun 23, 2016 at 8:13

3 Answers 3

1

You have to do 2 things.

  1. Use data-ng-bind-html=""
  2. Use $sce.trustAsHtml(string)

UPDATED: If you wont to use angular expressions, you have to compile them using

$compile.

You can read more via this $SCE

Sign up to request clarification or add additional context in comments.

Comments

0

I will tell you a long way but it will help you.Make a custom directive like this.

app.directive('dynamic', function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
  scope.$watch(attrs.dynamic, function(html) {
    ele.html(html);
    $compile(ele.contents())(scope);
  });
  }
   };
  });

Use as

<span  dynamic="{{htmlView}}" > 

1 Comment

so basically to convert string into html template i need to use $compile. right ??
0

Hi please check this fiddle https://plnkr.co/edit/iqNltdDYv2n9Agke0C2C?p=preview

HTML

  <div ng-controller="ExampleController">
      <p >{{newObj.eventTime}}</p>
       <p dynamic="htmlView"></p>
</div

and JS

(function(angular) {
  'use strict';
angular.module('bindHtmlExample', ['ngSanitize'])
  .controller('ExampleController', ['$scope', function($scope) {

       $scope.newObj = {
      billStatus : true,
      eventTime : "2015-01-10"
}

$scope.htmlView = '<p> {{newObj.eventTime}}</p> <div style="margin-top: -15px;">Hello <md-checkbox ng-checked="{{newObj.billStatus}}" style="margin-left: 0px;" aria-label="Bilable"><span style="margin-left:0px;">Bilable</span> </md-checkbox></div>'
  }])

  .directive('dynamic', function($compile) {
    return {
        restrict: 'A',
        replace: true,
        link: function (scope, element, attrs) {
            scope.$watch(attrs.dynamic, function(html) {
                element[0].innerHTML = html;
                $compile(element.contents())(scope);
            });
        }
    };
});
})(window.angular);

2 Comments

can u please explain the purpose of ngSanitize
that one no need if u remove also it will be work fine

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.