ReferenceError: function not defined

ReferenceError: function not defined

我正在尝试调用 javascript 方法。我正在通过字符串连接在 运行 时间构建 html。

$scope.getRoute = function (isRouteFormValid) {
    routingDemoPageService.executeService(serviceURL, 'admin', 'admin').then(function (response) {
        function tryingOnceAgain() {
            alert('called.....');
        }

        var markers = L.markerClusterGroup({
            showCoverageOnHover:false,
            chunkedLoading: true
        });

        var geojsonLayer = L.geoJson(response, {
            onEachFeature: function(feature, layer){

            var UIDValue = (feature.properties['uid'] !== null ? Autolinker.link(String(feature.properties['uid'])) : '');

            var popupContent = '<table>' +
                            '<tr><th scope="row"><a href="javascript:tryingOnceAgain()">Edit</a></th><td></td></tr>' +

                            '<tr><th scope="row">uid</th><td>' + UIDValue + '</td></tr></table>';

             layer.bindPopup(popupContent);
         }
     });
     markers.addLayer(geojsonLayer);
     $scope.map.addLayer(markers);
     $scope.map.fitBounds(markers.getBounds());

   })['catch'](function (error) {

 });

}

当我单击调用 tryingOnceAgain 方法的 link 时,出现以下错误

ReferenceError: tryingOnceAgain is not defined

我不确定为什么会出现以下错误。

有人可以提供任何指示我做错了什么吗?

javascript:tryingOnceAgain() 引用了全局范围内的函数,但您在 function (response) { 范围内定义了 tryingOnceAgain 函数。

要解决这个问题,您必须将 tryingOnceAgain 函数移动到全局范围。

或者在不改变物理位置的情况下将其分配给 window 对象:

window.tryingOnceAgain = function() {...}

您对 tryingOnceAgain() 的函数定义 仅存在于定义它的函数内部 ,在本例中为 $scope.getRoute().

这使得只有 $scope.getRoute() 内的代码可以调用 tryingOnceAgain() 并且它将 运行.

您必须在 $scope.getRoute() 之外定义 tryingOnceAgain() 或将其设为 public 属性 并像在 HTML 中那样调用它。