我的应用程序看起来像
var app = angular.module('cockpit', ['tenantService', 'ngMaterial', 'ngMdIcons']);
我的控制器看起来像
angular.module('cockpit').controller('TenantController', ['TenantService', function($scope, TenantService){
$scope.tenants = TenantService.get();
console.log('TenantController started', $scope.tenants);
}]);
我的服务看起来像
angular.module('tenantService', [])
.service('TenantService', function () {
this.get = function() {
return [
{
'name': 'fakeTenant',
'freeMemory': '45',
},
]
}
});
我正确导入依赖项
<script src="src/cockpit/app.js"></script>
<script src="src/cockpit/controllers/tenant_controller.js"></script>
<script src="src/cockpit/services/tenants.js"></script>
在我的HTML
,我做
<div ng-controller="TenantController">
{{tenants}}
</div>
但是当我看到console
时,我看到了
TypeError: Cannot read property 'get' of undefined
at new <anonymous> (tenant_controller.js:2)
at Object.e [as invoke] (angular.js:4169)
at G.instance (angular.js:8422)
at angular.js:7677
at r (angular.js:330)
at J (angular.js:7676)
at g (angular.js:7062)
at J (angular.js:7701)
at g (angular.js:7062)
at g (angular.js:7065)
为什么没有注入依赖?
答案 0 :(得分:0)
问题是控制器注入,以下修复
angular.module('cockpit').controller('TenantController', function($scope, TenantService){
$scope.tenants = TenantService.get();
console.log('TenantController started', $scope.tenants);
});