Last active
August 25, 2020 08:52
-
-
Save Sliqric7053/2c19ca325b75c471e39a93dcdef74801 to your computer and use it in GitHub Desktop.
Angular Services vs Models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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