I'm trying to create an Angular2 Component on the fly by
- Giving it a location
- Returning the component
I'm not using DynamicComponentLoader because it's deprecated and went for the ViewContainerRef
and the ComponentResolver
but I don't think the resolver is creating the component properly. It's only appending it at the end.
What am I doing wrong?
I've created a plnkr with the source code: http://plnkr.co/edit/a2esZiVpiV5f1r4uEufX?p=preview
@Component({
selector : 'marker-component',
template : '<strong>OH BABY ITS WORKING</strong>'
})
class MarkerComponent {
}
@Component({
selector: 'my-app',
template : `
<div id="map"></div>
`
})
export class App implements NgOnInit {
map: any
constructor(
private compiler: ComponentResolver,
private viewContainer: ViewContainerRef) {
}
ngOnInit() {
this.map = L.mapbox.map('map', 'mapbox.streets').setView([40, -74.50], 9);
var cssIcon = L.divIcon({
// Specify a class name we can refer to in CSS.
className: 'css-icon',
html: `<marker-component></marker-component>`,
iconSize: [60, 60]
});
L.marker([40, -74.50], {icon: cssIcon}).addTo(this.map);
this.compiler.resolveComponent(MarkerComponent).then((factory) =>
this.viewContainer.createComponent(factory, 0, this.viewContainer.injector));
}
}
bootstrap(App, [...HTTP_PROVIDERS]).catch(err => console.error(err));