我不明白为什么TestCntrl在我运行时是未定义的。我已经在父级中有一个现有的控制器(MainCntrl),现在我希望孩子拥有自己的控制器(TestCntrl)。
(function() {
'use strict';
angular
.module('app')
.config(function ($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider) {
$urlMatcherFactoryProvider.strictMode(false);
$urlRouterProvider.when('/', '/search');
$urlRouterProvider.when('/search/name', '/search');
$urlRouterProvider.otherwise('/');
$stateProvider
.state('index', {
url: '/',
abstract: true,
templateUrl: 'app/layout/layout.html',
controller: 'MainController',
controllerAs: 'main'
})
.state('index.layout', {
initFactory2: ['initFactory', function(initFactory) {
return initFactory.getClasses().then(function(data) {
return data.data;
});
}]
},
url: 'search',
views: {
'form@index': {
templateUrl: 'app/partials/form.html',
controller: 'TestController as test'
},
'results@index':{}
}
})
})();
的TestController
(function () {
'use strict';
angular
.module('app')
.controller('TestController', TestController);
/** @ngInject */
function TestController($scope, $stateParams, $state, model,
initFactory2) {
var vm = this;
vm.$scope = $scope;
}
});
主控制器
(function () {
'use strict';
angular
.module('app')
.controller('MainController', MainController);
/** @ngInject */
function MainController($scope, $stateParams, $state, $timeout, model,
SearchFactory) {
var vm = this;
}
})();
这是我得到的错误:
Error: [ng:areq] Argument 'TestController' is not a function, got undefined
http://errors.angularjs.org/1.3.17/ng/areq?p0=TestController&p1=not%20a%20function%2C%20got%20undefined
答案 0 :(得分:1)
您在TestController上的IIFE末尾缺少括号。这需要将其正确地声明为要执行的函数。没有它,您的TestController代码永远不会被调用,因此永远不会被实例化。
答案 1 :(得分:0)
问题在于你的 TestController
(function () {
'use strict';
angular
.module('app')
.controller('TestController', TestController);
/** @ngInject */
function TestController($scope, $stateParams, $state, model, initFactory2) {
var vm = this;
vm.$scope = $scope;
}
});
只是,尝试颠倒顺序
(function () {
'use strict';
/** @ngInject */
var TestController = function TestController($scope, $stateParams, $state, model, initFactory2){
var vm = this;
vm.$scope = $scope;
};
angular
.module('app')
.controller('TestController', TestController);
});