【问题标题】:How to call Angular function from checkbox onchange in leaflet popup html?如何从传单弹出html中的复选框onchange调用Angular函数?
【发布时间】:2021-07-07 05:49:27
【问题描述】:

我正在使用 Leaflet,但我的标记很少。因此,当将鼠标悬停在标记上时,会打开一个弹出窗口,在该弹出窗口中,有一个复选框。所以我需要的是每当用户点击该复选框时,它应该更改弹出内容。比如,

复选框名称是更多信息。默认弹出窗口包含悬停后的纬度和经度。当用户单击该复选框时,弹出窗口应显示城市、州以及纬度和经度。我在复选框的onchange 事件中创建了一个带有函数的复选框,但它没有调用创建的角度函数,而是在寻找JavaScript 函数。我怎样才能调用 Angular 函数或有其他解决方法来实现它?

我用过以下方法,

<input type="checkbox" onchange="showDetails(this);"/>
<input type="checkbox" onchange={{showDetails(this);}}/>

我的示例代码如下所示

L.Routing.control({
    waypoints: waypoints,
    lineOptions: {
      styles: [{color: '#453f30', opacity: 1, weight: 5}],
      addWaypoints: false,
      extendToWaypoints: false,
      missingRouteTolerance: 0,
    },
    plan: L.Routing.plan(waypoints, {
      createMarker: function (i, wp) {
        return L.marker([latitude, longitude], {
          icon: icon,
        }).bindPopup('<input type="checkbox" onchange={{showDetails(this);}}/>More Info', {
          closeButton: true,
        }).on("mouseover", function (event) {
          event.target.openPopup();
        });
      }
    })
  }).addTo(this.map);

【问题讨论】:

    标签: angular typescript leaflet leaflet-routing-machine


    【解决方案1】:

    您非常接近解决方案。您可以像上面一样使用与mouseover 事件相同的popupopen 事件。你的复选框应该是这样的。

    <input type="checkbox" id="chkbox1"/>
    

    然后按如下操作,

    }).bindPopup('<input type="checkbox" id="chkbox1"/>More Info', {
      closeButton: true,
    }).on("mouseover", function (event) {
      event.target.openPopup();
    }).on("popupopen", function (event) {
        // Here I am using JQuery. You can add JQuery to your Project
        $("#chkbox1").on("click", e => {
            e.preventDefault();
            
            if ($("#chkbox1").is(":checked")) {
                // your code goes here if checked
            } else {
                // 
            }
        });
    });
    

    【讨论】:

    • 非常感谢您的回复和详细的解释。同时也感谢您抽出时间来回答这个问题。
    猜你喜欢
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2011-01-28
    • 1970-01-01
    • 2019-04-17
    • 2013-10-19
    • 1970-01-01
    相关资源
    最近更新 更多