ngBindHtml不渲染清理HTML

时间:2015-07-23 15:24:35

标签: javascript angularjs ng-bind-html ng-bind ngsanitize

我有一个简单的角度应用程序,我正在尝试使用ngBindHtml将一些html注入页面。但是,它没有注入。这是我的HTML:

select * 
from your_table
where date between ('01/01/2013') and ('31/12/2013')
   or date between ('01/01/2015') and ('31/12/2015')

这是我的角色应用程序:

<main id="main" ng-bind-html="oreGen | trust"></main>

加载页面时,主要元素内没有任何内容。

这是一个codepen:http://codepen.io/trueScript/pen/MwbVpO?editors=101

您可以看到没有出现ngBinded的div。 为什么会这样?

1 个答案:

答案 0 :(得分:0)

您可以使用指令代替过滤器。请查看此JSFiddle

HTML:

<div ng-app="myApp" ng-controller="programController">
    <dir id="main" content="oreGen"></dir>
</div>

JS:

angular.module('myApp', [])
.controller('programController', ['$scope', '$filter', function ($scope, $filter) {
    $scope.oreGen = '<dir class="oreFunction" ng-click="collectFunction(\'parenthesis\', 1)">test text</dir>';
    $scope.collectFunction = function (value1, value2) {
        alert(value1 + value2);
    };
}])
.directive('dir', function ($compile, $parse) {
    return {
        restrict: 'E',
        link: function (scope, element, attr) {
            scope.$watch(attr.content, function () {
                element.html($parse(attr.content)(scope));
                $compile(element.contents())(scope);
            }, true);
        }
    }
});