Mohy Eldeen
Mohy Eldeen

Reputation: 1330

Angular 2 Component inside bootstrap popover

Since angular 2 is not coming with a full rich components, I decided to use bootstrap inside angular 2. I know this is not the best idea as it breaks the virtual dome issue, but I have no other solution. The problem that I am having is that angular 2 component will not render inside the popover html. Anyone have a solution for this?

I am looking at the Renderer class and it seems to be the solution for me, but I can not get it to work. Here is some code:

My event-view component that I need to render in the popover

import {Component, View} from 'angular2/core';
@Component({
    selector: 'event-view',
    template: '<div>Application details for application id: <h1>{{id}}</h1> will be loaded here!</div>',
    inputs: ['id']
})

export class EventView {
    id: string;
    constructor() {

    }
}

I think my solution will be at Renderer.renderComponent but I am not sure how can I user it.

Upvotes: 6

Views: 4956

Answers (1)

Leonel Franchelli
Leonel Franchelli

Reputation: 56

You could just simply

initialize a normal bootstrap popover like this

let popOver = $('[rel="popover"]');

popOver.popover({
    container: 'body',
    html: true,
    content: function () {
        return $('#myContent').removeClass('hide');
    }
}).click(function(e) {
    e.preventDefault();
});

popOver.on('show.bs.popover', (event) => {
    this.popover_initialized = true;
});

popOver.on('hide.bs.popover', (event) => {
    $(".popover[role=tooltip]").addClass('hide');

    event.preventDefault();
    event.stopImmediatePropagation();
});

then you just define a function to be called on the element that activates the popover

showToolTip(){
    if(!this.popover_initialized){
        return;
    }

    $(".popover[role=tooltip]").removeClass('hide');
}

So, the first time bootstrap initializes a normal popover, but we prevent the default of destroying the html content when it hides, then you simply add a 'hide' class to it, and then when you whant to activate it again you just simply remove that class, this works for angular directives, bindings, etc

Upvotes: 1

Related Questions