问题一、 iframe如何自适应屏幕高度

解决思路:通过设置iframe外层父元素高度等于window高度,再相对于父元素定位iframe元素;案例如下:

第一步: 模板文件中使用iframe


// demo.component.html
<div style="position: relative; " [style.height]="outHeight">
<iframe [src]="srcValue" allowtransparency="true" frameborder="0" id="defaulIframePage" style="position: absolute; width: 100%; height: 100%; "></iframe>
</div>

第二步: ts 中自定义iframe外层父元素的高度


// demo.component.ts
import {fromEvent} from "rxjs/index"; export class DemoComponent imple implements OnInit{ srcValue = 'http://www.baidu.com';
outHeight = '0px'; ngOnInit() {
// ifram最外层高度
this.outHeight = window.innerHeight + 'px';
fromEvent(window, 'resize').subscribe(($event) => {
this.outHeight = window.innerHeight + 'px';
});
}
}

问题二、 安全机制设置

错误:

解决:

第一步:创建管道


import { Pipe, PipeTransform } from '@angular/core';
import {DomSanitizer} from "@angular/platform-browser"; @Pipe({
name: 'safe'
})
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}
transform(value: any, args?: any): any {
return this.sanitizer.bypassSecurityTrustResourceUrl(value);
}
}

第二步: 在demo.component.html文件中加入管道


<iframe [src]="item.url | safe" allowtransparency="true" frameborder="0" id="defaulIframePage" style="position: absolute; width: 100%; height: 100%; "></iframe>

问题三、src值为同域名不同参数时,iframe不刷新问题

解决思路:使用动态组件 - 将iframe放至动态组件中,父组件将src传值给动态组件,并且每次传值时动态渲染组件;

1. 父组件定义


// parent.component.html
<a href= "javascript:;" (click)="loadCmp(srcArray[1])">切换iframe的src值</a>
<div #dynamic></div> // parent.component.ts
export class ParentComponentimplements OnInit, OnDestroy { // 动态切换的src模拟数据
srcArray = ["index.html?id='11'", "index.html?id='22'"]; // 动态组件
@ViewChild('dynamic', { read: ViewContainerRef }) dmRoom: ViewContainerRef;
currentCmp: any; // 当前渲染组件 constructor(private cfr: ComponentFactoryResolver) {} ngOnInit() {
// 动态渲染组件
this.loadCmp(this.srcArray[0]);
} // 动态渲染组件方法
loadCmp(srcValue) { const com = this.cfr.resolveComponentFactory(DynamicComponent);
this.dmRoom.clear(); // 清空视图
this.currentCmp = this.dmRoom.createComponent(com); // 传值
this.currentCmp.instance.pathUrl = srcUrl; }
}

2. 动态组件定义


// dynamic组件;;别忘记将DynamicComponent加入数组entryComponents中;
// dynamic.component.html
<iframe [src]="pathUrl | safe" allowtransparency="true" frameborder="0" id="defaulIframePage" style="position: absolute; width: 100%; height: 100%; "></iframe> // dynamic.component.ts
export class DynamicComponent {
pathUrl: string = '';
}

angular6 iframe应用的更多相关文章

  1. 完美判断iframe是否加载完成

    var iframe = document.createElement("iframe"); iframe.style.width = "265px"; ifr ...

  2. js学习笔记:操作iframe

    iframe可以说是比较老得话题了,而且网上也基本上在说少用iframe,其原因大致为:堵塞页面加载.安全问题.兼容性问题.搜索引擎抓取不到等等,不过相对于这些缺点,iframe的优点更牛,跨域请求. ...

  3. 页面嵌入dom与被嵌入iframe的攻防

    1.情景一:自己的页面被引入(嵌入)至别人的页面iframe中 if(window.self != window.top){ //url是自己页面的url window.top.location.hr ...

  4. iframe用法

    <iframe src="http://caiyanli.top/" height="500"  width="500" frameb ...

  5. 如何获取url中的参数并传递给iframe中的报表

    在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数 ...

  6. JavaScript权威设计--Window对象之Iframe(简要学习笔记十四)

    1.Window对象属性的文档元素(id) 如果在HTML文档中用id属性来为元素命名,并且如果Window对象没有此名字的属性,Window对象会赋予一个属性,它的名字是id属性的值,而他们的值指向 ...

  7. ASP.NET 页面禁止被 iframe 框架引用

    两个站点: a.sample.com b.sample.com a.sample.com 站点中的一段示例 JS 代码: var iframe = document.createElement(&qu ...

  8. 父页面操作iframe子页面的安全漏洞及跨域限制问题

    一.父子交互的跨域限制 同域情况下,父页面和子页面可以通过iframe.contentDocument或者parent.document来交互(彼此做DOM操作等,如父页面往子页面注入css). 跨域 ...

  9. 解决iframe作为子窗口,刷新后iframe页面跳转到其它页面的问题

    转载请在页首注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/5990262.html 前言: 在开发网站时,尤其是管理后台,我们经常会使用iframe作为内容窗 ...

随机推荐

  1. 转载-Spring Boot应用监控实战

    概述 之前讲过Docker容器的可视化监控,即监控容器的运行情况,包括 CPU使用率.内存占用.网络状况以及磁盘空间等等一系列信息.同样利用SpringBoot作为微服务单元的实例化技术选型时,我们不 ...

  2. HDU 5984 题解 数学推导 期望

    Let’s talking about something of eating a pocky. Here is a Decorer Pocky, with colorful decorative s ...

  3. KVC&KVO&运行时

    运行时:要先了解程序运行的三个阶段 1.编译阶段:clang将OC代码转换成C++,查看运行机制调用的方法 2.链接阶段:与我们使用到得库文件进行链接 3.运行阶段:我们要谈的运行时主要针对这个阶段, ...

  4. hdu6219 Empty Convex Polygons (最大空凸包板子

    https://vjudge.net/contest/324256#problem/L 题意:给一堆点,求最大空凸包面积. 思路:枚举凸包左下角点O,dp找出以这个点为起始位置能构成的最大空凸包面积, ...

  5. 【欧拉降幂】Super_log

    In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...

  6. 063 Python必备库-从人机交互到艺术设计

    目录 一.概述 二.Python库之图形用户界面 2.1 PyQt5 2.2 wxPython 2.3 PyGObject 三.Python库之游戏开发 3.1 PyGame 3.2 Panda3D ...

  7. Redis真的那么好用吗

    Redis是什么 Redis是一个开源的底层使用C语言编写的key-value存储数据库.可用于缓存.事件发布订阅.高速队列等场景.而且支持丰富的数据类型:string(字符串).hash(哈希).l ...

  8. BigDecimal转String

    代码: public static void main(String[] args) { // 浮点数的打印 System.out.println(new BigDecimal("10000 ...

  9. 史上最全 69 道 Spring 面试题和答案

    史上最全 69 道 Spring 面试题和答案 目录Spring 概述依赖注入Spring beansSpring注解Spring数据访问Spring面向切面编程(AOP)Spring MVC Spr ...

  10. 当递归遇到synchronized

    面试题:有一个synchronized方法,加入该方法发生递归调用,会导致线程死锁码? 解析: 所谓递归函数就是自调用函数,在函数体内直接或间接的调用自己,即函数的嵌套是函数本身. 递归方式有两种:直 ...