嵌入 angular 2 应用程序或其他任何事情都可以使用 IFrame 来实现:
import {Component, OnInit, ElementRef, ViewChild} from "@angular/core";
import {NavigationService} from "../navigation.service";
import {ActivatedRoute} from "@angular/router";
@Component({
selector: 'framed-content',
template: `
<div class="row content application-box">
<div class="col-xs-12 col-sm-12 col-md-12">
<iframe #iframe style="width: 100%;height: 100vh;position: relative;"
[src]="selectedContent | safe"
frameborder="0"
allowfullscreen></iframe>
</div>
</div>
`
})
export class FramedContentComponent implements OnInit {
@ViewChild('iframe') iframe: ElementRef;
private selectedContent = 'content/home.html';
constructor(private route: ActivatedRoute, private navigationService: NavigationService) {
}
/**
* Registers a callback for retrieving the target based on the selected content
*/
ngOnInit() {
this.route.params.map(params => params['selectedItem']).subscribe((selectedItem) => {
this.selectedContent = this.navigationService.getNavigationItem(selectedItem).target;
})
}
}
剩下的唯一问题是调整 IFrame 的大小以适应内容。
这可以使用容器应用程序的 de main index.html 中的一段纯 javascript 来解决:
<script language="JavaScript1.5">
window.addEventListener('message', function(event) {
// IMPORTANT: Check the origin of the data!
if (~event.origin.indexOf('http://localhost')) {
// The data sent with postMessage is stored in event.data
console.log(event.data);
resize(event.data);
} else {
// The data hasn't been sent from your site!
return;
}
});
function resize(frameHeight) {
console.log('Parent resize');
frame=document.getElementById('loketFrame');
console.log(frame);
frame.height=frameHeight;
}
</script>
嵌入式应用每次改变大小时,都需要向容器发送消息以更新 iframe 的高度:
ngOnInit() {
setTimeout(() => {
var height = document.documentElement.scrollHeight;
console.log('Resizing to ' + height);
parent.postMessage(height, '*');
}, 0);
}