好吧,我正在学习 AngularJS,其中一个功能是允许用户将 HTML 代码键入文本区域,这将被发送到 Controller 并存储在代码变量中。当用户按下按钮来测试代码时,我希望将文本插入到 HTML 文档中。目前它所做的只是将其添加为纯文本,而不是渲染它。
测试代码.template.js:
<button type="button" class="btn btn-primary" ng-click="$ctrl.showCode()">Test Code</button>
<button type="button" class="btn btn-primary" ng-click="$ctrl.clearCode()">Reset All</button>
{{$ctrl.codeShow}}
测试代码.component.js:
angular.
module('phonecatApp').
component('testCode', {
templateUrl: 'Components/test-code/test-code.template.html',
controller: [function TestCodeController() {
var self = this;
self.code = '';
self.codeShow = '';
self.showCode = function showCode()
{
self.codeShow = self.code;
}
self.clearCode = function clearCode()
{
self.codeShow = '';
self.code = '';
}
}]
});
澄清一下,按下按钮确实有效,并且在按下按钮时数据已成功从代码添加到 codeShow,并且它可以显示它,但它显示为明文而不是渲染。谢谢
最佳答案
尝试
<div ng-bind-html="$ctrl.codeShow"></div>
另外,请引用 https://docs.angularjs.org/api/ngSanitize/service/ $sanitize 以更好地实现
关于javascript - AngularJS 将文本转换为 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53776100/