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
| enum Statuses { | |
| Pending = 1, | |
| Approved = 2, | |
| Rejected = 3 | |
| } | |
| interface Order { | |
| status: Statuses; | |
| } |
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
| enum Statuses { | |
| Unread = 0, | |
| Read = 1 | |
| } | |
| @Component({ | |
| selector: 'component-with-enum', | |
| template: ` | |
| <div *ngFor="notification in notifications" | |
| [ngClass]="{'unread': notification.status == statuses.Unread}"> |
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
| enum Statuses { | |
| Unread = 0, | |
| Read = 1 | |
| } | |
| abstract class AbstractBaseComponent { | |
| statuses = Statuses; | |
| someOtherEnum = SomeOtherEnum; | |
| ... // lots of other reused stuff | |
| } |
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
| @Component({ | |
| selector: 'component-with-form', | |
| template: `...omitted for the sake of brevity` | |
| }) | |
| class ComponentWithForm extends AbstractBaseComponent { | |
| form: FormGroup; | |
| submitted: boolean = false; // a flag to be used in template to indicate whether the user tried to submit the form | |
| resetForm() { | |
| this.form.reset(); |
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
| abstract class AbstractFormComponent extends AbstractBaseComponent { | |
| form: FormGroup; | |
| submitted: boolean = false; // a flag to be used in template to indicate whether the user tried to submit the form | |
| resetForm() { | |
| this.form.reset(); | |
| } | |
| onSubmit() { | |
| this.submitted = true; |
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
| const routes: Routes = [ | |
| {path: 'user', component: UserContainerComponent} | |
| ]; | |
| @Component({ | |
| selector: 'user-container-component', | |
| template: `<app-user-component [user]="user"></app-user-component>` | |
| }) |
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
| <-- instead of this --> | |
| <div *ngFor="let user of users"> | |
| <h3 class="user_wrapper">{{user.name}}</h3> | |
| <span class="user_info">{{ user.age }}</span> | |
| <span class="user_info">{{ user.dateOfBirth | date : 'YYYY-MM-DD' }}</span> | |
| </div> | |
| <-- write this: --> | |
| <user-detail-component *ngFor="let user of users" [user]="user"></user-detail-component> |
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
| abstract class RestService { | |
| private baseUrl: 'http://your.api.domain'; | |
| constructor(private http: Http, private cookieService: CookieService){} | |
| private get headers(): Headers { | |
| /* | |
| * for example, add an authorization token to each request, | |
| * take it from some CookieService, for example |
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
| abstract class RestService { | |
| protected baseUrl: 'http://your.api.domain'; | |
| constructor(private http: Http, private cookieService: CookieService){} | |
| protected get headers(): Headers { | |
| /* | |
| * for example, add an authorization token to each request, | |
| * take it from some CookieService, for example |
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
| @Injectable() | |
| class UserService extends RestService { | |
| private relativeUrl: string = '/users/'; | |
| public getAllUsers(): Observable<User[]> { | |
| return this.get(this.relativeUrl); | |
| } | |
| public getUserById(id: number): Observable<User> { |