0

我正在使用一项服务来检索像这样绑定到 html 的数据

{{order.Notes}}

order.Notes 可能包含多行文本,所以我想使用 html 标签 'br' 对其进行格式化

这是我试图使用的指令:

angular.module('directives', [])

.directive('htmlText', function () {

return {
    restrict: 'E',
    template: '<div ng-bind-html-unsafe="html"></div>',
    scope: {
        text: '='
    },
    link: function (scope, elem, attrs) {

        var updateText = function () {

            if (scope.text != null) {
                scope.html = scope.text.replace('\n', "<br/>");
            }
        };

        scope.$watch('text', function (oldVal, newVal) {
            if (newVal) {
                updateText();
            }
        });
    }
};
});

<html-text text="order.Notes"></html-text>

但是,newVal 始终为空。我在这里想念什么?

4

0 回答 0