无法在bing地图中的图钉描述中发送onclick属性

时间:2016-06-29 14:51:38

标签: javascript jquery click bing-maps

我有一张带有bing地图的地图,上面有一些图钉,他们有html描述。我想点击这个描述(它是一个列表),调用外部函数。此函数用于创建HTML描述:

_getDescription = function (actions) {
    var description = "<div><ul class='action'>";
    actions.forEach(function (action) {
        description += '<li onclick="showOrderModal(action)"><strong>' + action.address.info.code + "</strong><br><span>" + action.address.info.address_for_pushpin + "</span>"
    })
    description += "</li></ul></div>"
    return description;
}

有了这个,我用我的描述设置了htmlContent:

$scope.pinInfobox.setOptions({
        // title: e.target.Title,
        htmlContent: e.target.Description,
        visible:true,
        offset: new Microsoft.Maps.Point(0,25)
    });

我有showOrderModal()功能,当我点击列表中的某个项目时,它会显示:

  

“未捕获的ReferenceError:未定义操作”

1 个答案:

答案 0 :(得分:1)

您看到的错误是有道理的。您有一个HTML字符串,其名称为属性,但没有实际的属性链接。在这个字符串中:

'<li onclick="showOrderModal(action)"><strong>'

操作只不过是一个字符串。当有人单击列表项时,操作值未定义,因为没有本地值。您需要做的是包含一些基本信息,可用于检索操作所需的信息。也许在某处存储动作数组,并将动作的索引传递到每个列表项的showOrderModal中。例如:

var currentActions;

_getDescription = function (actions) {
    currentActions = actions;

    var description = "<div><ul class='action'>";
    actions.forEach(function (action, idx) {
        description += '<li onclick="showOrderModal('+ idx + ')"><strong>' + action.address.info.code + "</strong><br><span>" + action.address.info.address_for_pushpin + "</span>"
    })
    description += "</li></ul></div>"
    return description;
}

function showOrderModal(idx){
    var action = currentActions[idx];

    //Do what ever you were doing before with the action.
}