Reputation: 636
The following code fails with an error:
readCandidates is not a function:
Here is the code
export class Candidates {
private _dataService : ModelContracts.IDataService;
constructor(private tag: ModelContracts.ITag, private dataService: ModelContracts.IDataService) {
this._value = tag;
this._dataService = dataService;
}
private _value : ModelContracts.ITag;
public get value() : ModelContracts.ITag {
return this._value;
}
public set value(v : ModelContracts.ITag) {
this._value = v;
}
candidates = [];
activate() {
this._dataService.readCandidates().then(candidates => this.candidates = candidates);
}
}
export interface IDataService {
readCandidates(): Promise<ModelContracts.ICandidate[]>
}
export class DataService {
//some implementation
}
I'm using Aurelia bet 1.1.0 and Typescript. The dataService dependency gets injected but the function call fails.
Upvotes: 1
Views: 85
Reputation: 250942
If the context of the call is not the class (for example, because you are calling activate
as a callback or from an event) you'll need to make sure you sort out your scope.
For example:
activate = () => {
this._dataService.readCandidates().then(candidates => this.candidates = candidates);
}
Although there are some better ways to do this if you want to have a consitent approach to handling the responsibility of scope in TypeScript. The recommendation is to handle scope off-class, as the class shouldn't know how it is going to be called.
Upvotes: 1