【问题标题】:Angular ng-click does not work after an exception in a ng-map directive在 ng-map 指令中出现异常后,Angular ng-click 不起作用
【发布时间】:2016-06-12 02:25:32
【问题描述】:

我在模态中使用 ng-map (http://ngmap.github.io/) 在我的应用程序中提供 googlemap,但在该指令出现异常后,页脚中的关闭按钮将不起作用。当我将关闭按钮放在指令之前(例如在标题中)时,它起作用了! 我应该如何捕获此类异常并保持控制?

我的模态如下:

 <script type="text/ng-template" id="views/OfficeMapModal.html">
        <div class="modal-header">
            <div class="modal-title">{{address}}</div>
        </div>
        <div class="modal-body">
            <ng-map zoom="15" center="{{center.latitude}}, {{center.longitude}}">
                <marker position="{{center.latitude}}, {{center.longitude}}"></marker>
            </ng-map>
        </div>
        <div class="modal-footer">
            <div class="btn btn-default select-again" data-ng-click="cancel()">close</div>
        </div>
 </script>

我的模态控制器如下:

angular.module('portalApp').controller('GoogleMapModalController', ['$scope', 'NgMap', '$uibModalInstance', 'address', 'center', function ($scope, NgMap, $uibModalInstance, address, center) {
$scope.address = address;

$scope.center = center;

NgMap.getMap().then(function (map) {
    google.maps.event.trigger(map, 'resize');
    var myLatLng = new google.maps.LatLng(center.latitude, center.longitude);
    map.setCenter(myLatLng);
});

$scope.cancel = function () {
    $uibModalInstance.dismiss('cancel');
};}]);

编辑:

错误:未定义 google t@http://localhost:3000/bower_components/ngmap/build/scripts/ng-map.min.js:1:2423

【问题讨论】:

  • 你能说明你遇到了什么错误吗?
  • 发生异常后应用程序无法按预期运行的事实对我来说听起来很正常...您是否尝试过避免或捕获此异常?
  • 我编辑了问题并添加了例外。我的问题是我的模态中的 ng-map 指令不是。

标签: javascript angularjs ng-map angular-ui-modal


【解决方案1】:

您正在调用 getMap() 方法,因此如果它在该方法中的任何地方失败且未处理错误,则执行将不会继续。这意味着 $scope.cancel 甚至不会被声明。

如果您预计此库中或特别是在调用 getMap() 的过程中会出现错误,理想情况下您应该尝试解决这些错误。但是,在由于无法控制的条件而无法解决错误的情况下保持取消按钮工作的最简单解决方案是将 $scope.cancel 声明移到 NgMap 初始化之上,在调用周围放置一个 try catch 并输出错误详情:

    angular.module('portalApp').controller('GoogleMapModalController', ['$scope', 'NgMap', '$uibModalInstance', 'address', 'center', function ($scope, NgMap, $uibModalInstance, address, center) {
    $scope.address = address;

   $scope.center = center;

   $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

   try { // safely attempt to initialize 
      NgMap.getMap().then(function (map) {
         google.maps.event.trigger(map, 'resize');
         var myLatLng = new google.maps.LatLng(center.latitude, center.longitude);
         map.setCenter(myLatLng);
      });
   } catch(err) { } // out put error
   }]);

【讨论】:

  • 问题不在控制器上。它在编译 ng-map 指令后立即出现在模态中
  • 您是否尝试过将 NgMap 初始化包装在 try catch 周围,您可能能够防止指令在页面上失败仍然给您一个有效的取消按钮,它可能不会显示完整的指令我猜的元素。
  • 我从控制器中删除了 NgMap.getMap 块,正如我之前所说的,它与 html 中的 ng-map 相关
  • 我不知道如何捕获或从 html 页面上的错误中恢复,即使它们是角度指令标签。我认为潜在的问题仍然存在于控制器中的某个地方或角度本身(不会从指令中的失败继续向下翻页),因为这种情况不应该发生在网页上。对不起,我不能再帮上忙了。最后一项检查是您在控制台中是否收到任何错误?
  • 我也会尝试在没有脚本标签的情况下对模态进行编码,因为那是在这段代码中进行角度运行和呈现 html。尝试使用原始 html 来排除问题是 text/ng-template 类型的脚本标记。
猜你喜欢
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 2013-05-05
相关资源
最近更新 更多