angular - SafeValue 必须使用 [property]=binding 虽然我已经在使用属性绑定(bind)

标签 angular sanitization property-binding

我有以下带有属性绑定(bind)的 HTML:

<div [innerHtml]="logOutput"></div>

在我的组件中,我现在用这行代码添加一些内容
this.logOutput = this.sanitizer.bypassSecurityTrustHtml( this.logOutput + otpStr );

但是我还是收到了这个错误“SafeValue must use [property]=binding”。

为什么我收到这个错误?我已经在使用属性绑定(bind)了!我正在使用 Angular 5。

编辑:我尝试在 HTML 中使用自定义管道,效果很好,但我想要一个没有管道的解决方案。

编辑2:

这是我设置 HTML 的函数,也许有帮助。一个完整的工作示例是不可能的,因为我的应用程序处理命令行工具和输出流(我在电子框架中有 Angular)
this.logStream.on('data', (chunk) => {
  let otpStr = chunk.toString();
  if (otpStr == '') {
    return;
  }

  otpStr = this.StylePipe(otpStr); // Convert ANSI Styles to HTML with CSS
  otpStr = otpStr.replace(/\r?\n/g, '<br />');
  otpStr = otpStr.replace(/<br \/><br \/>/g, '<br />');
  otpStr = otpStr.replace(/^<br \/>/, '');
  otpStr = otpStr.replace(/<br \/>$/, '');
  this.logOutput = this.sanitizer.bypassSecurityTrustHtml( this.logOutput + otpStr + '<br />' );
  this.ref.detectChanges();
});

this.logStream.on('end', () => {
  this.logOutput = this.sanitizer.bypassSecurityTrustHtml( this.logOutput + '<br />' );
  this.ref.detectChanges();
});

最佳答案

我现在在评论中的@JB Nizet 的帮助下解决了这个问题。我现在使用两个变量。第一个是包含我生成的原始 HTML 的辅助变量,另一个变量包含绑定(bind)到 HTML 的经过 sanitizer 的 HTML,因为您无法连接 SafeHtml(绕过的结果)和字符串。

this.logOutputHtml += otpStr;
this.logOutput = this.sanitizer.bypassSecurityTrustHtml( this.logOutputHtml );

关于angular - SafeValue 必须使用 [property]=binding 虽然我已经在使用属性绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48474173/

相关文章:

javascript - Angular 5 keyup 事件被触发两次

ruby-on-rails - 清理 URL slug 的 Unicode 字符串 (Ruby/Rails)

php - 在 PHP 中清理 UTF-8

java - 为什么将 TextField 绑定(bind)到正在另一个线程上更新的属性最终会导致应用程序抛出错误?

javascript - NgStyle 中的相同属性多个值

javascript - 以 Angular 6 重试 HTTP 请求

javascript - 浏览器如何或从何处获取 "sourcemapped"javascript 文件的源文件?

java - ReadOnlyBooleanWrapper : incorrect behaviour when used with Bindings. 或

javascript - 在 Angular 5 中使用 Stripe

javascript - 你可以在没有指令的情况下在 ngModel 上动态渲染 HTML 吗?