How to communicate and external HTML with angular 2 container app











up vote
0
down vote

favorite












Using Angular 2 I've created an application that needs to load an external html, to achieve this I did a simple node api to serve the external html and finally render this external file into my angular 2 application. This is what I want, that works perfectly.



app.component.html



<main>
<h1>Hi, from the container</h1>
<test-component></test-component> <!-- The external html -->
<main>


myExternalFile.html



<main>
<h2>Hi, Im the external file</h2>
</main>


test.component.ts



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

}


So this works, after that I can see the html loaded without any problem. Now I need to add a button with an action that will be processed on the angular2 container.



Like adding a button in the external html (myExternalFile.html)



<main>
<h2>Hi, Im the external file</h2>
<button (click)="hi()">say hi!</button>
</main>


And adding the method (test.component.ts)



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

// New method
hi() {
console.log('we made connection!')
}

}


But, I'm not getting any message on my console. How can I stablish this kind of connection? since everything is already compiled... adding and external file in this ways makes me thing more about this kind of communication.










share|improve this question






















  • First I don't know why you need to do this and keep in mind this a security issue. But you can check Angular Compiler service. An example that can help you to understand it : stackoverrun.com/fr/q/13017753
    – Gilsdav
    Nov 7 at 21:57

















up vote
0
down vote

favorite












Using Angular 2 I've created an application that needs to load an external html, to achieve this I did a simple node api to serve the external html and finally render this external file into my angular 2 application. This is what I want, that works perfectly.



app.component.html



<main>
<h1>Hi, from the container</h1>
<test-component></test-component> <!-- The external html -->
<main>


myExternalFile.html



<main>
<h2>Hi, Im the external file</h2>
</main>


test.component.ts



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

}


So this works, after that I can see the html loaded without any problem. Now I need to add a button with an action that will be processed on the angular2 container.



Like adding a button in the external html (myExternalFile.html)



<main>
<h2>Hi, Im the external file</h2>
<button (click)="hi()">say hi!</button>
</main>


And adding the method (test.component.ts)



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

// New method
hi() {
console.log('we made connection!')
}

}


But, I'm not getting any message on my console. How can I stablish this kind of connection? since everything is already compiled... adding and external file in this ways makes me thing more about this kind of communication.










share|improve this question






















  • First I don't know why you need to do this and keep in mind this a security issue. But you can check Angular Compiler service. An example that can help you to understand it : stackoverrun.com/fr/q/13017753
    – Gilsdav
    Nov 7 at 21:57















up vote
0
down vote

favorite









up vote
0
down vote

favorite











Using Angular 2 I've created an application that needs to load an external html, to achieve this I did a simple node api to serve the external html and finally render this external file into my angular 2 application. This is what I want, that works perfectly.



app.component.html



<main>
<h1>Hi, from the container</h1>
<test-component></test-component> <!-- The external html -->
<main>


myExternalFile.html



<main>
<h2>Hi, Im the external file</h2>
</main>


test.component.ts



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

}


So this works, after that I can see the html loaded without any problem. Now I need to add a button with an action that will be processed on the angular2 container.



Like adding a button in the external html (myExternalFile.html)



<main>
<h2>Hi, Im the external file</h2>
<button (click)="hi()">say hi!</button>
</main>


And adding the method (test.component.ts)



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

// New method
hi() {
console.log('we made connection!')
}

}


But, I'm not getting any message on my console. How can I stablish this kind of connection? since everything is already compiled... adding and external file in this ways makes me thing more about this kind of communication.










share|improve this question













Using Angular 2 I've created an application that needs to load an external html, to achieve this I did a simple node api to serve the external html and finally render this external file into my angular 2 application. This is what I want, that works perfectly.



app.component.html



<main>
<h1>Hi, from the container</h1>
<test-component></test-component> <!-- The external html -->
<main>


myExternalFile.html



<main>
<h2>Hi, Im the external file</h2>
</main>


test.component.ts



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

}


So this works, after that I can see the html loaded without any problem. Now I need to add a button with an action that will be processed on the angular2 container.



Like adding a button in the external html (myExternalFile.html)



<main>
<h2>Hi, Im the external file</h2>
<button (click)="hi()">say hi!</button>
</main>


And adding the method (test.component.ts)



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

// New method
hi() {
console.log('we made connection!')
}

}


But, I'm not getting any message on my console. How can I stablish this kind of connection? since everything is already compiled... adding and external file in this ways makes me thing more about this kind of communication.







javascript angular






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 21:37









RicardoGonzales

97121738




97121738












  • First I don't know why you need to do this and keep in mind this a security issue. But you can check Angular Compiler service. An example that can help you to understand it : stackoverrun.com/fr/q/13017753
    – Gilsdav
    Nov 7 at 21:57




















  • First I don't know why you need to do this and keep in mind this a security issue. But you can check Angular Compiler service. An example that can help you to understand it : stackoverrun.com/fr/q/13017753
    – Gilsdav
    Nov 7 at 21:57


















First I don't know why you need to do this and keep in mind this a security issue. But you can check Angular Compiler service. An example that can help you to understand it : stackoverrun.com/fr/q/13017753
– Gilsdav
Nov 7 at 21:57






First I don't know why you need to do this and keep in mind this a security issue. But you can check Angular Compiler service. An example that can help you to understand it : stackoverrun.com/fr/q/13017753
– Gilsdav
Nov 7 at 21:57














1 Answer
1






active

oldest

votes

















up vote
0
down vote













<main>
<h2>Hi, Im the external file</h2>
<button id="mybtn" (click)="hi()">say hi!</button>
</main>


app.component.ts



import { Component, Input, Pipe } from '@angular/core';
import { Http } from '@angular/http';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'test-component',
template: `<div [innerHTML]="myExternalHTML"></div>`
})
export class TestComponent {

myExternalHTML: any = "";

constructor(http: Http, private sanitizer: DomSanitizer ) {
http.get('http://localhost:5000/api/v1/todos') // my basic node app
.subscribe((response: any) => {
const externalHTML=
this.sanitizer.bypassSecurityTrustHtml(response.text());
this.myExternalHTML= externalHTML;
}, (error: any) => {
console.log('Error: ' + error);
})
}

ngAfterViewInit(){
let el = document.getElementById('mybtn') as HTMLElement;
el.click();
}
// New method
hi() {
console.log('we made connection!')
}

}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198203%2fhow-to-communicate-and-external-html-with-angular-2-container-app%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    <main>
    <h2>Hi, Im the external file</h2>
    <button id="mybtn" (click)="hi()">say hi!</button>
    </main>


    app.component.ts



    import { Component, Input, Pipe } from '@angular/core';
    import { Http } from '@angular/http';
    import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

    @Component({
    selector: 'test-component',
    template: `<div [innerHTML]="myExternalHTML"></div>`
    })
    export class TestComponent {

    myExternalHTML: any = "";

    constructor(http: Http, private sanitizer: DomSanitizer ) {
    http.get('http://localhost:5000/api/v1/todos') // my basic node app
    .subscribe((response: any) => {
    const externalHTML=
    this.sanitizer.bypassSecurityTrustHtml(response.text());
    this.myExternalHTML= externalHTML;
    }, (error: any) => {
    console.log('Error: ' + error);
    })
    }

    ngAfterViewInit(){
    let el = document.getElementById('mybtn') as HTMLElement;
    el.click();
    }
    // New method
    hi() {
    console.log('we made connection!')
    }

    }





    share|improve this answer

























      up vote
      0
      down vote













      <main>
      <h2>Hi, Im the external file</h2>
      <button id="mybtn" (click)="hi()">say hi!</button>
      </main>


      app.component.ts



      import { Component, Input, Pipe } from '@angular/core';
      import { Http } from '@angular/http';
      import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

      @Component({
      selector: 'test-component',
      template: `<div [innerHTML]="myExternalHTML"></div>`
      })
      export class TestComponent {

      myExternalHTML: any = "";

      constructor(http: Http, private sanitizer: DomSanitizer ) {
      http.get('http://localhost:5000/api/v1/todos') // my basic node app
      .subscribe((response: any) => {
      const externalHTML=
      this.sanitizer.bypassSecurityTrustHtml(response.text());
      this.myExternalHTML= externalHTML;
      }, (error: any) => {
      console.log('Error: ' + error);
      })
      }

      ngAfterViewInit(){
      let el = document.getElementById('mybtn') as HTMLElement;
      el.click();
      }
      // New method
      hi() {
      console.log('we made connection!')
      }

      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        <main>
        <h2>Hi, Im the external file</h2>
        <button id="mybtn" (click)="hi()">say hi!</button>
        </main>


        app.component.ts



        import { Component, Input, Pipe } from '@angular/core';
        import { Http } from '@angular/http';
        import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

        @Component({
        selector: 'test-component',
        template: `<div [innerHTML]="myExternalHTML"></div>`
        })
        export class TestComponent {

        myExternalHTML: any = "";

        constructor(http: Http, private sanitizer: DomSanitizer ) {
        http.get('http://localhost:5000/api/v1/todos') // my basic node app
        .subscribe((response: any) => {
        const externalHTML=
        this.sanitizer.bypassSecurityTrustHtml(response.text());
        this.myExternalHTML= externalHTML;
        }, (error: any) => {
        console.log('Error: ' + error);
        })
        }

        ngAfterViewInit(){
        let el = document.getElementById('mybtn') as HTMLElement;
        el.click();
        }
        // New method
        hi() {
        console.log('we made connection!')
        }

        }





        share|improve this answer












        <main>
        <h2>Hi, Im the external file</h2>
        <button id="mybtn" (click)="hi()">say hi!</button>
        </main>


        app.component.ts



        import { Component, Input, Pipe } from '@angular/core';
        import { Http } from '@angular/http';
        import { BrowserModule, DomSanitizer } from '@angular/platform-browser';

        @Component({
        selector: 'test-component',
        template: `<div [innerHTML]="myExternalHTML"></div>`
        })
        export class TestComponent {

        myExternalHTML: any = "";

        constructor(http: Http, private sanitizer: DomSanitizer ) {
        http.get('http://localhost:5000/api/v1/todos') // my basic node app
        .subscribe((response: any) => {
        const externalHTML=
        this.sanitizer.bypassSecurityTrustHtml(response.text());
        this.myExternalHTML= externalHTML;
        }, (error: any) => {
        console.log('Error: ' + error);
        })
        }

        ngAfterViewInit(){
        let el = document.getElementById('mybtn') as HTMLElement;
        el.click();
        }
        // New method
        hi() {
        console.log('we made connection!')
        }

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 22:48









        Ahmad mnzr

        786319




        786319






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198203%2fhow-to-communicate-and-external-html-with-angular-2-container-app%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings