angular - 在另一个 Angular 2 应用程序中嵌入一个 Angular 2 应用程序

标签 angular iframe embed

我的任务是实现一个基于 Angular 2 的应用程序 提供页眉、导航栏和页脚。所有非常标准的东西。 但是在导航栏之间,其他angular 2应用可以 显示哪个应用程序取决于用户在导航中的选择 酒吧。 嵌入式 Angular 2 应用程序通过它们自己的 URL 访问。 导航栏是使用来自 允许轻松配置的 REST API。

听起来这可以使用 IFRAME 实现,但是有吗? 使用 IFRAME 的任何替代方法,以及使用 浏览器的前后按钮、键盘快捷键等。

我可以动态调整 IFRAME 大小以适应嵌入的大小吗? Angular 应用程序?即使嵌入式应用程序的大小发生变化?

最佳答案

使用 IFrame 可以实现嵌入 angular 2 应用程序或任何其他相关内容:

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);
}

关于angular - 在另一个 Angular 2 应用程序中嵌入一个 Angular 2 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460436/

相关文章:

javascript - ngx-quill/quill.js 从innerHTML中去除自定义印迹

JavaScript 无法删除 DIV

html - iFrame src 中的 HSTS

javascript - iFrame 中的 Service Worker

fonts - Windows 字体可清楚地消除数字零与字母 O 的歧义

python - 如何嵌入成员(member)的图像和昵称? (w/discord.py)

angular - 数据表问题 - ERROR TypeError : val. slice is not a function

javascript - 从嵌入的 Plunker/StackBlitz 等代码编辑器发送和获取数据

Angular2 和 typescript : How to add string extension method/prototype

html - YouTube 嵌入视频播放器尺寸仍然太小