$ sce.trustAsHtml does not work

Ok, so the following below, which I tested in code:

JavaScript:

var string = '<p>hello</p>';
$scope.html = $sce.trustAsHtml(string);

HTML:

<span ng-bind-html="html"></span>

What I'm really trying to do is this: pull a bunch of items from the server in JSON form. One of the JSON keys is called "description", which is just a long line of html code.

I am transferring these elements to the $ scope array, so I can display them on the page using the ng-repeat directive. The code below summarizes how this is done:

JavaScript:

$.getJSON('someURL', function(data) {
    $scope.items = [];
    for (var i = 0; i < data.count; i++) {
        var item = {};
        item.description = $sce.trustAsHtml(data.item[i].description);
        $scope.items.push(item);
    }
});

HTML:

<p ng-repeat="item in items">
    <span ng-bind-html="item.description"></div>
</p>

For some reason this does not produce any conclusion. Something I read was that any variable you bind $ sce.trustAsHtml () should be a $ scope variable. In this case, I set it to the regular variable "item.description", and then add it to the $ scope.item array.

, - , , .

- ?

!

+4
2

<span ng-bind-html="'{{item.description}}'"></span>.

- .

+8

AngularJS 1.4.9 :

:

.controller('MyCtrl', ['$scope', '$sce',
    function($scope, $sce) {
        $scope.infoText = $sce.trustAsHtml('My <strong>HTML text</strong>');
    }
])

:

<span ng-bind-html="infoText"></span>
+3

Source: https://habr.com/ru/post/1546372/


All Articles