我正在使用typescript实现一个简单的角度应用程序。
我将服务引用传递给控制器,但是当我尝试调用服务的方法时,我得到一个异常,说服务引用是未定义的。 “dataAccessLayer”模块在“clinicManager”模块(控制器所属的模块)中正确添加为依赖项。
我的服务:
module app.dal{
interface IDataAccessService{
get(id:number):app.Model.IPatient;
}
export class DataAccessService implements IDataAccessService{
private _db : Loki;
static $inject = ['Loki'];
constructor(private Loki:Loki) {
this._db = new Loki('./database/db.json',{
autosave: true,
autosaveInterval : 3000 // 3 secs
});
}
get(id:number):app.Model.IPatient {
// Implementation here...
}
}
angular.module('dataAccessLayer',['lokijs'])
.service('dataAccessService',['Loki',DataAccessService]);
}
控制器:
module app.PatientDetails{
interface IPatientDetails{
patient: app.Model.IPatient;
}
import accessLayer = app.dal.DataAccessService;
export class PatientDetailsCtrl implements IPatientDetails{
private dataAccessService: accessLayer;
static $inject = ['patient','dataAccessService'];
constructor(public patient:app.Model.IPatient, dataAccessService:accessLayer){
this.dataAccessService = dataAccessService;
// -> TypeError: Cannot read property 'get' of undefined
var patient = dataAccessService.get(1);
}
}
angular.module('clinicManager')
.controller('patientDetailsCtrl',['dataAccessService',PatientDetailsCtrl]);
}
app.js
angular.module('clinicManager',['ngMaterial','dataAccessLayer']);
的index.html
<!-- Application scripts-->
<script src="app.js"></script>
<!-- Services-->
<script src="DataAccessLayer/DataAccessService.js"></script>
<!-- Controllers-->
<script src="Controllers/PatientDetailsCtrl.js"></script>
答案 0 :(得分:0)
如果我在控制器中删除了对Patient的引用,该服务可以正常实例化,该服务正常工作。我在这里放置代码以更好地显示格式:
module app.PatientDetails{
interface IPatientDetails{
// patient: app.Model.IPatient;
}
import accessLayer = app.dal.DataAccessService;
export class PatientDetailsCtrl implements IPatientDetails{
private dataAccessService: accessLayer;
static $inject = [/*'patient',*/'dataAccessService'];
constructor(/*public patient:app.Model.IPatient,*/ dataAccessService:accessLayer){
this.dataAccessService = dataAccessService;
// The function is now called properly
var patient = dataAccessService.get(1);
}
}
angular.module('clinicManager')
.controller('patientDetailsCtrl', ['dataAccessService',PatientDetailsCtrl]);
}