Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular 2 / leaflet map,如何从标记弹出窗口链接到组件? … routerLink?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的角度2应用程序中,我有一个传单映射,弹出窗口绑定到onClick事件.

弹出窗口的内容一个指向角度组件的链接.但是当我在.setContent()函数中使用routerLink时,链接不会显示.

我猜这种情况正在发生,因为.setContent()无法呈现有意义的angular 2指令.我可以用什么呢?

@Component({
  SELEctor: 'app-map',templateUrl: './map.component.html',styleUrls: ['./map.component.css']
})

export class MapComponent implements AfterViewInit {

  openmap: any;

  constructor() { }

  ngAfterViewInit() {

    let openmap = l.tileLayer("@R_197_10107@://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}",{
      attribution: 'terms and FeedBACk'
    });

    let map = l.map("map",{
      center: [33.2148,-97.1331],zoom: 5,zoomControl: true,maxZoom: 18 
    }).addLayer(openmap);

    let marker = l.marker([39.2148,-98.1331]).addTo(map);

    let popup = l.popup();

    function onMapClick(E) {
      popup
        .setLatLng(e.latlng)
        .setContent("Facility" + "<br/>" + "<a routerLink='/view2'>" + "View Two" + "</a>")
        .openOn(map);
    }

    map.on('click',onMapClick);
  }

}

针,如果我把它更改为

.setContent("Facility" + "<br/>" + "<a href='../view2'>" + "View Two" + "</a>")

会做我想要的,但这会导致页面刷新,所以这不是一个选项.

解决方法

一个非常简单的方法一个非常复杂的方法.

简单的方法是在没有RouterLink的情况下使用带有angular元素的原始HTMl.注册该锚点元素的点击并使用路由器服务进行导航.

任务是发射链接,但实际问题更深入,现在它链接下次显示一个角度组件…

因此,对于复杂的解决方案:

这是一个非常高级的主题……不仅涉及使用先进的角度技术,它在传单实现中也是先进的.

我将尽力传达信息,但由于复杂性,示例将非常简单并且需要工作.

第一 – 角度领域.

包含指令,组件或管道的HTML字符串永远不会起作用,唯一的方法是初始化View

让我们将A View定义为查看组件或模板实例的参.

这些被称为ComponentRef和TemplateRef

所以,我们有两种方法可以解决这个问题.由于我不能同时使用COR_318_11845@ponentRef,但请注意您也可以使用TemplateRef.
使用模板,您首先需要获取组件中定义的模板以及将该模板附加到的ViewContainerRef.

我们将构建一个接受传单Marker并绑定到标记的click事件的服务,点击它将打开一个弹出窗口,它是一个角度组件.

组件很简单,它呈现链接.

@Component({
  SELEctor: 'facility-link',template: `Facility <br/> <a routerLink="{{link}}"> View Two</a>`
})
export class FacilityLinkComponent {
  public link: String;
  constructor() { }
}

现在,为服务:

@Injectable()
export class LinkPopupservice {

  constructor(private cfr: ComponentFactoryResolver,private injector: Injector,private appRef: ApplicationRef) { }


  register(marker: leaflet.Marker,link: String): void  {
    marker.on('click',($event: leaflet.MouseEvent)  => this.popup($event.target,link) );
  }

  popup(marker: leaflet.Marker,link: String) {
    const cR_318_11845@pFactory = this.cfr.resolveComponentFactory(FacilityLinkComponent);
    const componentRef = cmpFactory.create(this.injector);
    componentRef.instance.link = link;
    this.appRef.attachView(componentRef.hostView);
    const markerElement = marker.getElement();
    markerElement.parentElement.appendChild(componentRef.LOCATIOn.nativeElement);

    const markerPos = leaflet.DomUtil.getPosition(markerElement);
    const markerClass = leaflet.DomUtil.getClass(markerElement);


    leaflet.DomUtil.setTransform(componentRef.LOCATIOn.nativeElement,markerPos);
    leaflet.DomUtil.setClass(componentRef.LOCATIOn.nativeElement,markerClass);
  }
}

register方法接受标记链接注册click事件.

当弹出方法触发时,它使用角度工具创建FacilityLinkComponent的视图实例,设置未来绑定的链接,将视图附加到它并将其附加到DOm.

这一切都发生在前5行代码中.

一些说明:

>我们必须附加一个视图,以便更改检测工作
>正确的实现将允许设置ViewContainerRef和/或Injector – 这是使用延迟加载时必须的.
>最好通过Injector将数据发送到组件,而不是通过赋值(ReflectiveInjector)
>需要正确清理(破坏组件并分离视图)
>需要添加切换逻辑,也可以在导航上清理.

传单

第6行的代码执行弹出窗口的定位.

这是一个非常简单的逻辑,它只是复制标记中的所有内容.

这就是我使用标记的原因,所以我将有一个来定位.

一个真实世界的例子中,你需要获得一个面板并将组件推入他们自己的层,计算位置.这并不困难,因为传单有所有的帮助,但它太多了.

希望能帮助到你.

大佬总结

以上是大佬教程为你收集整理的Angular 2 / leaflet map,如何从标记弹出窗口链接到组件? … routerLink?全部内容,希望文章能够帮你解决Angular 2 / leaflet map,如何从标记弹出窗口链接到组件? … routerLink?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。