-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] Angular 2

, 20 2017 . 18:19 +


Angular 2 : popup leaflet. , .


. angular 2+ , leaflet.js. MapService , . MapComponent . webpack 2. , , popup :

marker.bindPopup(`
    	    

Leaflet PopUp

Some text

Should be deleted from DOM if it was angular component because of ngIf = false

`);


:

   popup

DOM Should be deleted from DOM..., *ngIf, popup , . .


, :

@Component({
    selector: 'custom-popup',
    template: require('./custom-popup.component.html')
})
export class CustomPopUpComponent {
	public inputData: any;

	private title: string = 'Angular component';
	private array: Array = ['this', 'array', 'was viewed', 'by', 'ngFor!'];
}

template:

{{title}}

{{inputData}}

{{text}}

Should be deleted from DOM if it was angular component because of ngIf = false


dynamic-render.service.ts:

@Injectable()
export class RenderService {

	private componentRef: ComponentRef;

	constructor(private ngZone: NgZone,
	            private injector: Injector,
	            private appRef: ApplicationRef,
	            private componentFactoryResolver: ComponentFactoryResolver) { }

	public attachCustomPopUpsToMap(map: Map) {
		this.ngZone.run(() => {
			map.on("popupopen",
			       (e: any) => {
				       const popup = e.popup;

				       const compFactory = this.componentFactoryResolver.resolveComponentFactory(popup.options.popupComponentType);
				       this.componentRef = compFactory.create(this.injector);

				       this.componentRef.instance.geoObject = popup.options.object;

				       this.appRef.attachView(this.componentRef.hostView);

				       let div = document.createElement('div');
				       div.appendChild(this.componentRef.location.nativeElement);

				       popup.setContent(div);
			       });
		});
	}
}

addListener , . popup componentFactory, , options. instance , options popup. inputData options.data. div , popup.

: angular 2.3.0+. .

 this.appRef.attachView(this.componentRef.hostView);



if (this.appRef['attachView']) {
	this.appRef['attachView'](this.componentRef.hostView);
	this.componentRef.onDestroy(() => {
		this.appRef['detachView'](this.componentRef.hostView);
	});
}
else {
	this.appRef['registerChangeDetector'](this.componentRef.changeDetectorRef);
	this.componentRef.onDestroy(() => {
		this.appRef['unregisterChangeDetector'](this.componentRef.changeDetectorRef);
	});
}

RenderService MapModule. MapModule declarations entryComponents CustomPopUpComponent. renderService popup , :

this.renderService.attachCustomPopUpsToMap(this.mapService.getMap());
let options = {
		    data: 'you can provide here anything you want',
		    popupComponentType: CustomPopUpComponent
	    };
let myPopUp = L.popup(options);
marker.bindPopup(myPopUp);

data , popupComponentType . , , . , . , , popup leaflet:

  !


popup leaflet angular 2+. /, leaflet.

, .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331298/

:  

: [1] []
 

:
: 

: ( )

:

  URL