AngularJS:TypeError:无法读取未定义的属性'get'?
16lz
2021-01-22
My App looks like
我的App看起来像
var app = angular.module('cockpit', ['tenantService', 'ngMaterial', 'ngMdIcons']);
My controller looks like
我的控制器看起来像
angular.module('cockpit').controller('TenantController', ['TenantService', function($scope, TenantService){
$scope.tenants = TenantService.get();
console.log('TenantController started', $scope.tenants);
}]);
and my service looks like
我的服务看起来像
angular.module('tenantService', [])
.service('TenantService', function () {
this.get = function() {
return [
{
'name': 'fakeTenant',
'freeMemory': '45',
},
]
}
});
I import the dependencies correctly as
我正确导入依赖项
<script src="src/cockpit/app.js"></script>
<script src="src/cockpit/controllers/tenant_controller.js"></script>
<script src="src/cockpit/services/tenants.js"></script>
and in my HTML
, I do
在我的HTML中,我做到了
<div ng-controller="TenantController">
{{tenants}}
</div>
But when I see console
I see
但是当我看到控制台时,我看到了
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)
Why is the dependency not injected?
为什么没有注入依赖?
1 个解决方案
#1
0
The problem was injection at controller, the following fixed it
问题是控制器注入,以下修复它
angular.module('cockpit').controller('TenantController', function($scope, TenantService){
$scope.tenants = TenantService.get();
console.log('TenantController started', $scope.tenants);
});
更多相关文章
- 从json获取最大插槽并应用于控制器
- 【问题解决方案】ImportError: No module named 'pygal'
- Python 黏包及黏包解决方案
- 无法安装ndg-httpsclient或者我的解决方案错误
- Linux无法连接网络解决方案
- Media-S 简介(一个开源的DRM解决方案)
- AppScan安全问题解决方案
- PLSQL乱码解决方案
- 数据库不支持中文解决方案(mysql)