【发布时间】: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