Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No html tags are working if the parent tag contain ng-bind-html directive

Following is my Code Example, if i removed ng-bind-html then the div is working, if not it fails, please suggest me an answer

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular-sanitize.js"></script>

<script>
    var app = angular.module('myApp', ['ngSanitize']);

app.controller('CartController', function($scope) {
                $scope.title = "<p>hello world</p>";
            $scope.test = "How are you..!How are you..!How are you..!";
        });
  </script>

<body ng-app="myApp" >
    <div ng-controller="CartController">
    <label ng-bind-html=title> 
        <div>{{test}}</div>
    </label>
    </div>
</body>
</html>
like image 290
Mohan Raj Avatar asked Dec 02 '25 09:12

Mohan Raj


2 Answers

You need to use $sce.trustAsHtml injecting $sce into the controller:

JSFiddle

app.controller('CartController', function($scope, $sce) {
    $scope.title = $sce.trustAsHtml("<p>hello world</p>");
    $scope.test = "How are you..!How are you..!How are you..!";
});

Of course the HTML label will be completely replaced by $scope.title and you aren't able to see {{test}} anymore

like image 135
michelem Avatar answered Dec 05 '25 02:12

michelem


When u use ng-bind-html to the <label> it completely replaced by the $scope variable, so u cannot achieve the <div> inside the <label> tag if the label contains ng-bind-html directive

<label ng-bind-html='<p>hello world</p>'> Label1 <div>text1</div> </label>

In the above Html, Only the html "hello world" will be displayed along with text1, anyway it is not advisable until it requires..!

like image 23
Mohan Raj Avatar answered Dec 05 '25 03:12

Mohan Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!