AddDevicemodalCtrl不是函数,未定义

时间:2016-06-07 13:59:09

标签: javascript angularjs angularjs-service angularjs-controller ng-app

我收到上面提到的错误,我相信这是因为控制器未正确连接到模块,但我可能错了。这是已定义的控制器。

(function () {
'use strict';

angular
  .module('WS')
  .controller('AddDeviceModalCtrl', AddDeviceModalCtrl);

/* @ngInject */
function AddDeviceModalCtrl(
  $rootScope,
  $scope,
  $stateParams,
  close,
  Auth,
  device,
  DeviceAPI,
  PathfinderAPI,
  helpers,
  GLOBAL_CONST,
  Notification
) {...})();

我单击网页上的一个按钮,它会点击该控制器并点击openModal功能。 Modal对象是一个定义如下的服务。

(function() {
  'use strict';

  angular
    .module('WS.environment')
    .controller('DevicesCtrl', DevicesCtrl);

  /* @ngInject */
  function DevicesCtrl($rootScope, $scope, $state, $stateParams, DeviceAPI, helpers, GLOBAL_CONST, Modal) {
    angular.extend($scope, {
      openModal: openModal,
      editDevice: editDevice
    });

    angular.extend($scope, {
      devices: [],
      errors: []
    });

    $rootScope.$on('deviceAdded', onUpdateDeviceList);

    DeviceAPI
      .getDevices()
      .then(onGetDeviceListSuccess);

    function onGetDeviceListSuccess(devices) {
      $scope.devices = devices;
    }

    $rootScope.$on('updateDevicesEvent', function(event, devices) {
      onGetDeviceListSuccess(devices);
    });

    function openModal($event) {
      Modal.showAddDevice($scope.device);
    }

    function editDevice($event, device) {
      Modal.showEditDevice(device);
    }

    function onUpdateDeviceList($event, device) {
      $scope.devices.push(device.device);
    }
  }

})();

这是我的服务:

(function() {
  'use strict';

  angular
    .module('WS.service')
    .factory('Modal', Modal);

  /* @ngInject */
  function Modal($rootScope, ModalService) {
    var service = {
      showAddDevice: showAddDevice,
      showEditDevice: showEditDevice,
      showConfirmation: showConfirmation,
      showTimeRange: showTimeRange
    };

    return service;

    function showAddDevice(device) {
      $rootScope.modalHasOpen = true;

      return ModalService.showModal({
        templateUrl: 'modules/modals/device/add/views/device.html',
        controller: 'AddDeviceModalCtrl',
        inputs: {
          device: device
        }
      });
    }})();

这是我的应用:

WS.app = angular
  .module('WS', [
    'interceptors',
    'WS.common',
    'WS.account',
    'WS.layout',
    'WS.dashboard',
    'WS.environment',
    'WS.service',
    'WS.settings'
  ])
  .config(config)
  .run(run);

1 个答案:

答案 0 :(得分:0)

当您向其添加AddDeviceModalCtrl控制器时,似乎'WS'模块不可用。 请注意,在添加任何控制器或服务之前,您必须确保角度模块已准备就绪。

在单个文件中尝试以下代码:

(function () {
'use strict';

WS.app = angular
  .module('WS', [
    'interceptors',
    'WS.common',
    'WS.account',
    'WS.layout',
    'WS.dashboard',
    'WS.environment',
    'WS.service',
    'WS.settings'
  ])
  .config(config)
  .run(run);

angular
  .module('WS')
  .controller('AddDeviceModalCtrl', AddDeviceModalCtrl);

/* @ngInject */
function AddDeviceModalCtrl(
  $rootScope,
  $scope,
  $stateParams,
  close,
  Auth,
  device,
  DeviceAPI,
  PathfinderAPI,
  helpers,
  GLOBAL_CONST,
  Notification
) {...})();