【问题标题】:data attribute of object tag in html is not updating the new urlhtml中对象标签的数据属性没有更新新的url
【发布时间】:2018-10-13 23:52:14
【问题描述】:

问题: 我正在尝试动态更新对象标签的数据属性。 问题是它第一次完美加载,但此后即使当我检查数据属性值在使用 Angular 的 html 代码中完美更新时,它也没有。我相信它不会被刷新。

HTML代码:

<object id="content" *ngIf="download" [data]="newUrl" width="70%" height="300px"></object>

角度代码:

  updateUrl(url: string) {
    console.log('Parent Component updateUrl: ' + url);
    this.download = false; // hide

    this.newUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);
    document.getElementById("content").setAttribute('data', url);    
    this.download = true; // show 
  }

我尝试了什么: 在更新 url 之前隐藏/显示,但不起作用。

请注意我不能使用 jQuery。任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 你试过 [attr.data]="newUrl" 吗?

标签: javascript angular html typescript


【解决方案1】:

您应该避免在 Angular 中使用纯 JavaScript 代码,否则 Angular 将无法感知更改。进行以下更改 -

html代码

<object id="content" *ngIf="download" [attr.data]="newUrl" width="70%" height="300px"></object>

角度代码

 updateUrl(url: string) {
    console.log('Parent Component updateUrl: ' + url);
    this.download = false; // hide
    this.newUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url); 
    this.download = true; // show 
  }

演示

Woking 副本在这里https://stackblitz.com/edit/angular-object-url-pdf

【讨论】:

  • 确保 pdf url 来自安全协议 https。演示链接在这里stackblitz.com/edit/angular-object-url-pdf
  • 我正在加载一个外部网页,目前它不是 https。另请注意,我不能使用 iFrame。
  • @Dev,我认为这行不通。在这种情况下,您可能会遇到混合内容错误,因为您的网站将在 HTTPS 上加载,而您的外部网页将在 http 上加载。
  • 我的网站不会在 https 上加载,iFrame 链接也不是 https url。两者都是http。
  • 那么它将在您的网站中运行,但是如果您在任何https 站点中测试它,它将无法运行。
【解决方案2】:

像这样更新数据属性,而不是使用 Javascript 更新。

<object id="content" *ngIf="download" [attr.data]="newUrl" width="70%" height="300px"></object>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 2014-04-10
相关资源
最近更新 更多