Javascript ng map指令中出现异常后,角度ng单击不起作用

Javascript ng map指令中出现异常后,角度ng单击不起作用,javascript,angularjs,ng-map,angular-ui-modal,Javascript,Angularjs,Ng Map,Angular Ui Modal,我在一个模式中使用了ng map()在我的应用程序中提供googlemap,但在该指令中出现异常后,我在页脚中的close按钮将无法工作。当我把关闭按钮放在指令之前(例如在标题中)时,它就工作了! 我应该如何捕捉这些异常并保持控制 我的语气如下: <script type="text/ng-template" id="views/OfficeMapModal.html"> <div class="modal-header"> &l

我在一个模式中使用了ng map()在我的应用程序中提供googlemap,但在该指令中出现异常后,我在页脚中的close按钮将无法工作。当我把关闭按钮放在指令之前(例如在标题中)时,它就工作了! 我应该如何捕捉这些异常并保持控制

我的语气如下:

 <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@


您正在调用方法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
   }]);

你能告诉我你犯了什么错误吗?在一次异常后,应用程序不能按预期工作,这对我来说很正常。。。您是否尝试过避免或捕获此异常?我编辑了问题并添加了异常。我的问题是模态not中的ng map指令。问题不在控制器中。它在编译ng map指令后立即在模式中发生。如果您尝试将NgMap初始化包装为一个try catch,您可能可以防止指令在页面上失败,但仍会提供一个工作取消按钮,我想它可能不会显示完整的指令元素。我从控制器中删除了NgMap.getMap块,正如我之前所说的,它与htmlI中的ng map相关,我不知道如何捕获html页面上的错误或从错误中恢复,即使它们是角度指令标记。我认为潜在的问题在于控制器本身的某个地方(不是从指令中的故障继续向下翻页),因为这种情况不应该发生在网页上。对不起,我在这件事上帮不了你。最后一个检查是控制台中是否有任何错误?我还会尝试在不使用script标记的情况下编写model,因为在这段代码中运行和呈现html是有角度的。尝试使用原始html排除类型为text/ng template的问题脚本标记。