您可以按照pkozlowski-opensource 的回答将控制器注册为模块的一部分。
如果您需要缩小,您可以通过在列表中的实际函数之前提供变量名来简单地扩展它:
angular.module('[module name]', []).
controller('PhoneListCtrl', ['$scope', function($scope) {
$scope.phones = [..];
$scope.orderProp = 'age';
}]);
这在“缩小”之后也会起作用:
angular.module('[module name]', []).
controller('PhoneListCtrl', ['$scope', function(s) {
s.phones = [..];
s.orderProp = 'age';
}]);
这个符号可以在Dependency Injection的“内联注释”下找到。
要测试已注册为模块一部分的控制器,您必须要求 Angular 创建您的控制器。例如:
describe('PhoneListCtrl test', function() {
var scope;
var ctrl;
beforeEach(function() {
module('[module name]');
inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('[module name]', {$scope: scope});
});
});
it('should be ordered by age', function() {
expect(scope.orderProp).toBe('age');
});
});
这种测试控制器的方法可以在Understanding the Controller Component的“测试控制器”下找到。