获取错误类型错误:onOk 不是一个函数

Getting Error TypeError: onOk is not a function

本文关键字:函数 一个 类型 取错误 错误 onOk 获取      更新时间:2023-09-26

我想创建构造对话框,以便在将数据发送到数据库之前符合要求,但是得到这个TypeError: onOk is not a function,onOk()定义了确认服务,请检查一下。

  app.directive('confirmDialog', function(ConfirmService) {
      return {
          restrict: 'A',
          scope: {
              eventHandler: '&ngClick'
          },
          link: function(scope, element, attrs){
            element.unbind("click");
            element.bind("click", function(e) {
              ConfirmService.open(attrs.confirm, scope.eventHandler, 'confirm_dialog');
            });
          }
      }
  });
  app.service('ConfirmService', function($modal) {
    var service = {};
    service.open = function (text, onOk, dialog_type) {
      var modalInstance = $modal.open({
        templateUrl: 'templates/dialog.html',
        controller: 'ConfirmCtrl',
        resolve: {
          text: function () {
            return text;
          }
        }
      });
      modalInstance.result.then(function (selectedItem) {
        onOk();
      }, function () {
      });
    };    
    return service;
  })
  app.controller('ConfirmCtrl', function ($scope, $modalInstance, text) {
    $scope.text = text;
    $scope.ok = function () {
      $modalInstance.close(true);
    };
    $scope.cancel = function () {
      $modalInstance.dismiss('cancel');
    };
  });

您还没有展示如何使用指令,但我非常怀疑这是否会起作用,因为 ngClick 是一个指令,而不是一个函数,而您似乎正在作为服务中的onOk参数传递这一点。

scope: {
    eventHandler: '&ngClick'
},