【问题标题】:Object property value sanitization problem in AngularAngular中的对象属性值清理问题
【发布时间】:2021-06-06 14:22:48
【问题描述】:

我有一个类 MemberInfo。有一个属性是照片。现在我想创建一个具有属性值的 memberinfo 对象。代码如下:

    let info = new MemberInfo();
    info.MemberId = "101";
    info.MemberName = "Mohammad Nasir Uddin";
    info.Type = "General";
    info.Category = "A";
    info.BloodGroup = "B+";
    info.MobileNo = "012589652357";
    info.Photo = this.sanitizer.bypassSecurityTrustUrl("'https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50'");  
    info.ShopNo = "101";
    info.ShopName = "Yellow Computers";
    info.Floor = "First Floor";

this.selectedMember = info;

为了制作安全的图片网址,我已经对其进行了清理。但它显示了编译时错误:

类型“SafeUrl”不可分配给类型“字符串”。信息.照片 = this.sanitizer.bypassSecurityTrustUrl("'https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50'")

要将照片绑定到 html,我使用了以下代码:

<img [src]="selectedMember.Photo" class="mr-2">

我该如何解决这个问题?

【问题讨论】:

    标签: angular sanitization


    【解决方案1】:

    DomSanitizer bypassSecurityTrustUrl 方法从 @angular/platform-b​​rowser 命名空间返回一个 SafeUrl 对象。

    现在将 MemberInfo 类中的 Photo 属性修改为 SafeUrl 类型或 any 类型。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2010-11-25
      • 1970-01-01
      • 2011-03-13
      • 2019-09-10
      • 2018-04-28
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多