Skip to content

Instantly share code, notes, and snippets.

@Sliqric7053
Last active August 25, 2020 08:52
Show Gist options
  • Save Sliqric7053/2c19ca325b75c471e39a93dcdef74801 to your computer and use it in GitHub Desktop.
Save Sliqric7053/2c19ca325b75c471e39a93dcdef74801 to your computer and use it in GitHub Desktop.
Angular Services vs Models
export class DmsDocumentModel {
...
isReference: boolean;
...
constructor(data: any, $injector: ng.auto.IInjectorService) {
const refService = $injector.get("refService") as RefService; // Non-angular class injects Angular services
this._isReference();
}
private _isReference(): boolean {
this.isReference = refService.isRef(); // this makes an XHR call to the backend etc.
}
}
// The keys are default (defined in the class), the values come from the backend. An example:
export class DocumentModel {
 ...   
isReference: boolean; 
...
constructor(data: any, $injector: ng.auto.IInjectorService) {
refService = $injector.get("refService") as RefService; // My gripe - think non-angular classes shouldn't inject Angular services
this._isReference(); 
}
 private _isReference(): boolean {
   this.isReference = refService.isRef(); // this makes an http call to the backend
}
}
// So the injected services are basically fetching data and assigning it to the keys.
export class User {
private id: number;
private firstName: string;
constructor(id: number, firstName: string, public http: HttpClient) {
[this.id](http://this.id) = id;
this.firstName = firstName;
}
get avatar() {
return this.http.get(`/users/${[this.id](http://this.id)}/avatar`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment