r/Angular2 • u/Dazzling_Chipmunk_24 • Mar 14 '26
How to embed an iframe in Angular
So I used this code to embed an iframe in Angular.
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
export class AppComponent {
externalUrl: SafeResourceUrl;
constructor(private sanitizer: DomSanitizer) {
this.externalUrl = this.sanitizer.bypassSecurityTrustResourceUrl(
'https://external-app.com'
);
}
} <div class="iframe-container">
<iframe [src]="externalUrl" width="100%" height="800"></iframe>
</div>But keep getting this error
Framing 'website name' violates the following Content Security Policy directive: "frame-ancestors 'self'". The request has been blocked.
How do I get around this problem?
0
Upvotes
6
u/lcriscola Mar 14 '26
That is probably the external app web server setting the CRS headers
2
u/BhootErBap Mar 14 '26
why don't you use proxy? https://angular.dev/tools/cli/serve#proxying-to-a-backend-server, I hope you will get the idea it's doesn't mean you need backend all the time 👍
2
u/fightmen007 Mar 15 '26
If the https://external-app.com doesn't allow to be iframed, you cannot do anything basically.
The error you are getting is saying to you "Only I can iframe myself" or in other words, https://external-app.com can iframe itself only.
1
u/kuros33 Mar 14 '26
You either don’t (as the other site tells your browser to block it) or build a proxy on your backend to access the remote site.
0
u/ldn-ldn Mar 14 '26
<iframe src="https://external-app.com/" />
0
u/[deleted] Mar 15 '26
[deleted] — view deleted comment