如何在Angular中在iframe无法加载src中的资源时显示替代文本。

huangapple go评论66阅读模式
英文:

How to show alternative text if Iframe can't load the resource at src in Angular

问题

I have a running Angular 12 application and I am implementing a behavior where I need to fetch the content from an external site and display it inside the modal popup. I am using <iframe src="url"> to fetch the content from a separate application. If the website server is running, I can successfully fetch the content and display it inside the modal.

但是,如果服务器宕机,会显示以下错误信息:

localhost connection refused

我想要实现一种行为,即如果服务器宕机,而不是显示 "localhost connection refused",在模态框中显示消息 "服务器宕机"。

display-content.component.ts

contentUrl: string;
serverDown: string;

constructor(private sanitizer: DomSanitizer) { }

ngOnInit(): void {
  const url = "http://localhost:5000/getContents";
  this.contentUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);
}

onIframeError(event) {
 console.log('iframe failed');
}

display-content.component.html

<iframe [src]="contentUrl" frameborder="0" class="content" (load)="onIframeError($event)"></iframe>
<p *ngIf="serverDown">服务器宕机</p>
英文:

I have a running Angular 12 application and I am implementing a behavior where I need to fetch the content from external site and display inside the modal popup. I am using &lt;iframe src=&quot;url&quot;&gt; to fetch the content from separate application. If the website server is on, I am able to fetch the content and display inside the modal.

But, if the server is down, its showing:

localhost connection refused

I want to implement a behavior where if the server is down; show the legend "Server is down" in the modal instead of localhost connection refused.

display-content.component.ts

contentUrl: string;
serverDown: string;

constructor(private sanitizer: DomSanitizer) { }

ngOnInit(): void {
  const url= &quot;http://localhost:5000/getContents&quot;; 
  this.contentUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);
}

onIframeError(event) {
 console.log(&#39;iframe failed&#39;);
}

display-content.component.html

&lt;iframe [src]=&quot;contentUrl &quot; frameborder=&quot;0&quot; class=&quot;content&quot; (load)=&quot;onIframeError($event)&quot;&gt;&lt;/iframe&gt;
&lt;p *ngIf=&quot;serverDown&quot;&gt;Server is down&lt;/p&gt;

答案1

得分: 2

以下是代码的中文翻译部分:

display-content.component.ts

    contentUrl: string;
    serverDown: boolean | undefined;
    
    constructor(private sanitizer: DomSanitizer, private _http: HttpClient) {
        const url= "http://localhost:5000/getContents"; 
        this.contentUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);

        this._http.get(url, { observe: 'response' })
          .pipe(first())
          .subscribe(resp => {
             if (resp.status === 200 ) {
               this.serverDown = false;
             } else {
               this.serverDown = true;
             }
           }, err => this.serverDown = true);
         }

display-content.component.html

    <iframe *ngIf="serverDown === false" [src]="contentUrl " frameborder="0" class="content"></iframe>
    <img src="loadingImage" *ngIf="serverDown === undefined"/>
    <p *ngIf="serverDown">Server is down</p>

希望这对你有帮助。如果有其他问题,请随时提出。

英文:

you can try this :

display-content.component.ts

contentUrl: string;
serverDown: boolean | undefined;

constructor(private sanitizer: DomSanitizer, private _http: HttpClient) {
    const url= &quot;http://localhost:5000/getContents&quot;; 
    this.contentUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);

    this._http.get(url, { observe: &#39;response&#39; })
      .pipe(first())
      .subscribe(resp =&gt; {
         if (resp.status === 200 ) {
           this.serverDown = false;
         } else {
           this.serverDown = true;
         }
       }, err =&gt; this.serverDown = true);
     }

display-content.component.html

&lt;iframe *ngIf=&quot;serverDown === false&quot; [src]=&quot;contentUrl &quot; frameborder=&quot;0&quot; class=&quot;content&quot;&gt;&lt;/iframe&gt;
&lt;img src=&quot;loadingImage&quot; *ngIf=&quot;serverDown === undefined&quot;/&gt;
&lt;p *ngIf=&quot;serverDown&quot;&gt;Server is down&lt;/p&gt;

huangapple
  • 本文由 发表于 2023年2月27日 00:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定