angularjs html代码到指令中的符号不​​起作用

时间:2014-10-01 08:16:56

标签: angularjs angularjs-directive

crmApp.directive('udArrow', function () {
return {
    Restrict: 'A',
    link: function (scope, element, attrs) {
        var upArrow = '↑';
        var downArrow = '&#8595';
        scope.resultTemplate = '';
        if (attrs.val1 > attrs.val2) {
            scope.resultTemplate =(upArrow);
        }
        else {
            scope.resultTemplate = (downArrow);
        }
    },
    template: '<span>{{resultTemplate}}</span>'
}

});

结果是(我故意分开分号而不是构成箭头) &amp;#x2191;

我也非常欣赏更好的解决方案。 :)

1 个答案:

答案 0 :(得分:1)

使用ng-bind-html

crmApp.directive('udArrow', function () {
return {
    Restrict: 'A',
    link: function (scope, element, attrs) {
        var upArrow = '&#x2191;';
        var downArrow = '&#8595';
        scope.resultTemplate = '';
        if (attrs.val1 > attrs.val2) {
            scope.resultTemplate =(upArrow);
        }
        else {
            scope.resultTemplate = (downArrow);
        }
    },
    template: '<span><div ng-bind-html="resultTemplate"></div></span>'
}