简体   繁体   English

html中的对象标签的data属性未更新新的url

[英]data attribute of object tag in html is not updating the new url

Issue: I am trying to update the data attribute of object tag dynamically. 问题:我正在尝试动态更新对象标签的数据属性。 The issue is the first time it loads perfectly, but thereafter it does not even though when i inspect the data attribute values are updated perfectly in html code using Angular. 问题是它第一次完美加载,但是此后即使我检查数据属性值是否使用Angular在html代码中完美更新也没有。 I believe it's not getting refreshed. 我相信它不会刷新。

Html Code: HTML代码:

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

Angular Code: 角度代码:

  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 
  }

What i Tried: Hide/Show before updating the url, but does not work. 我尝试过的方法:在更新url之前隐藏/显示,但不起作用。

Please note i cannot use jQuery. 请注意,我无法使用jQuery。 Any help would be greatly appreciated.Thanks! 任何帮助将不胜感激。谢谢!

You should avoid pure javascript code inside the angular otherwise angular will not be the aware of changes. 您应该避免在angular中使用纯JavaScript代码,否则angular不会意识到更改。 Make the below changes - 进行以下更改-

html code HTML代码

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

Angular Code 角度代码

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

Demo 演示

Woking copy is here https://stackblitz.com/edit/angular-object-url-pdf 沃金的副本在这里https://stackblitz.com/edit/angular-object-url-pdf

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM