css - 使用来自 Nodejs 的 Angularjs 格式化文本文件

标签 css angularjs node.js html twitter-bootstrap

我在后端使用 nodejsexpress 读取文本文件的内容。

当我点击服务时,我在前端获取数据。

Object {data: "s1:- hi thank you for calling my name is oh…↵s1:- no worries your welcome bye mam
↵s2:- bye
↵", status: 200, config: Object, statusText: "OK"}

我能够获取文本,但我需要根据一些关键字(例如 s1 和 s2)对这些文本进行格式化,并添加换行符、动态加粗并使用 angular 显示该数据

最佳答案

第一个建议是,如果您有权访问该文件并控制内容,也许您可​​以使用 Markdown,然后使用如下指令:angular-markdown-directive

如果您不能更改内容,则必须创建自己的指令,以有效地查找和替换内容中的那些标签并将它们转换为 HTML。然后您可以使用 ng-bind-html 来呈现输出。

它看起来像:

angular.module('specialParse', [])
    .directive('specialParse', function() {
        return {
            template: '<div ng-bind-html="content"></div>',
            link: function(scope, element, attr) {
                scope.content = attr.specialParse
                    .replace(/\r?\n/g, '<br />') // Replace line breaks
                    .replace('s1', 'something') // Special tag
                    .replace('s2', 'somethingElse'); // Special tag
                    // ..... And so on.
            }
        }
    });

和 HTML

<div special-parse="data"></div>

关于css - 使用来自 Nodejs 的 Angularjs 格式化文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32048215/

相关文章:

javascript - 使用 NodeJs 在 Aerospike 中批量导入

c++ - Qt 和 Webkit 未显示所有 CSS 3、HTML 5 效果

angularjs - AngularJS 中元素的指令模板唯一 ID

jquery - Angularjs Controller : how to call a controller in another controller?

mysql - 如何将sql server连接到app.js文件?

javascript - ESLint/Nodejs : Error linting due to default switch case

javascript - 将鼠标悬停在标记上时不要更改鼠标图标

JavaScript 按钮值和进度条

html - 如何让DIV占据底部的绝对定位容器的剩余空间

angularjs - 测量 API 调用所花费的时间