【问题标题】:Controller not recognized in AngularJS 1.5.7AngularJS 1.5.7 中无法识别控制器
【发布时间】:2017-03-08 15:39:16
【问题描述】:

新项目的另一个令人不安的情况:

以下控制器代码:

(function () {
'use strict';

angular
    .module('offerRequest', [])
    .controller('offerRequestCtrl', offerRequestCtrl);

offerRequestCtrl.$inject = ['$scope', '$rootScope', '$location'];

function offerRequestCtrl($scope, $rootScope, $location) {
    // controller code starts here.

    /* jshint validthis:true */
    var vm = this;
    vm.title = 'offerRequestCtrl';

    activate();

    function activate() { }
}
})();

使用以下路由:

  // state for requesting offers
    .state('offerrequest', {
        url: '/offerrequest',
        templateUrl: 'app/components/offerrequest/offerrequest.html',
        controller: 'offerRequestCtrl'
    })

导致以下错误消息:

   Argument 'offerRequestCtrl' is not a function, got undefined

尝试了旧的定义方式:

   angular.module('offerRequest', [])
    .controller('offerRequestCtrl', function offerRequestCtrl() {

    });

抛出同样的错误。在我想使用的第一个实现中,我可能做错了什么?

【问题讨论】:

  • 你在其他地方用过.module('offerRequest', [])吗?
  • 你能提供一个 plunkr 或 jsfiddle 吗?
  • @satpal 是的,我有,在我正在使用的另一项服务中。这是不正确的方式吗?我删除了服务,它仍然抛出同样的错误。
  • (function () { 'use strict'; angular .module('offerRequest', []) .factory('offerRequestService', offerRequestService); offerRequestService.$inject = ['$location']; function offerRequestService($location) { // controller code starts here. /* jshint validthis:true */ var vm = this; vm.title = 'offerRequestService'; activate(); function activate() { } } })();
  • 模块只能定义一次

标签: angularjs angular-controller


【解决方案1】:

您需要仅在全局范围内声明具有依赖关系的模块,

angular
    .module('offerRequest', [])

将您的控制器更改为,

angular
    .module('offerRequest')
    .controller('offerRequestCtrl', offerRequestCtrl);

【讨论】:

  • 我从服务中删除了依赖项,但没有从控制器中删除,现在它可以工作了。感谢您的帮助。
  • 4 分钟后我才能接受新答案。对不起,我会在那之后。
  • @Satpal 好吧,我们提醒其他人接受答案的传统;)
  • @Sajeetharan 是我们提醒其他人接受答案的传统;)
  • @SaurabhAgrawal,之前给 OP 相同的松弛和时间来接受。如果 OP 从未接受过答案,那么它可以,否则它的恕我直言乞求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2016-05-30
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多