簡體   English   中英

html中的對象標簽的data屬性未更新新的url

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

問題:我正在嘗試動態更新對象標簽的數據屬性。 問題是它第一次完美加載,但是此后即使我檢查數據屬性值是否使用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。 任何幫助將不勝感激。謝謝!

您應該避免在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 
  }

演示

沃金的副本在這里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