Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

javascript - How to run a component's method in angular from iframe as its container

here is angular code
testing.component.ts

import { Component, NgZone, OnInit } from '@angular/core';
import {data} from '../../assets/data.js';
import { IPost } from '../_models/IPost.js';

import {WindowRefService} from '../WindowRef.service';


@Component({
  selector: 'app-testing',
  templateUrl: './testing.component.html',
  styleUrls: ['./testing.component.css']
})
export class TestingComponent implements OnInit {
  
  constructor() { }

  ngOnInit(): void { }

  callInIframe() {
    console.log('Call In Iframe');
  }

}

In routine manner such code does not lunch in any container like iframe, however I want to lunch in iframe. Something like this:
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body> 
   <!--Angular app load in iframe , below code -->
    <iframe src="/" frameborder="0" width="100%" height="900px"></iframe>
    <script>
        **I want to Call Angular method here**
    </script>
</body>
</html>

how can I Call callInIframe() in script tag of index.html. @gzoechi

question from:https://stackoverflow.com/questions/65858808/how-to-run-a-components-method-in-angular-from-iframe-as-its-container

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add the iframe on the template of the main component instead of the index.html

app.component.html

<hello name="{{ name }}"></hello>
<p>
    this is an iframe for may website on an angular project
</p>
<iframe src="https://mansourcodes.com/"></iframe>

See Example

https://stackblitz.com/edit/angular-ivy-nqec9u?file=src/app/app.component.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...