Skip to content

Commit 329db74

Browse files
IsmaestroIsmael Ramos
authored andcommitted
feat(oninit): moved logic parts from constructors to ngOnInit method
1 parent fc2f15f commit 329db74

File tree

7 files changed

+37
-19
lines changed

7 files changed

+37
-19
lines changed

src/app/app.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {Component} from '@angular/core';
1+
import {Component, OnInit} from '@angular/core';
22
import {TranslateService} from '@ngx-translate/core';
33
import {Meta, Title} from '@angular/platform-browser';
4-
54
import {NavigationEnd, Router} from '@angular/router';
65
import {AppConfig} from './config/app.config';
76
import {MatSnackBar} from '@angular/material';
@@ -13,17 +12,19 @@ declare const Modernizr;
1312
templateUrl: './app.component.html'
1413
})
1514

16-
export class AppComponent {
15+
export class AppComponent implements OnInit {
1716

18-
isOnline = navigator.onLine;
17+
isOnline: boolean;
1918

2019
constructor(private translateService: TranslateService,
2120
private title: Title,
2221
private meta: Meta,
2322
private snackBar: MatSnackBar,
2423
private router: Router) {
24+
this.isOnline = navigator.onLine;
25+
}
2526

26-
this.translateService = translateService;
27+
ngOnInit() {
2728
this.translateService.setDefaultLang('en');
2829
this.translateService.use('en');
2930

src/app/core/footer/footer.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import {TranslateService} from '@ngx-translate/core';
88
})
99

1010
export class FooterComponent {
11+
1112
currentLang: string;
12-
currentDate = Date.now();
13+
currentDate: number;
1314

1415
constructor(private translateService: TranslateService) {
1516
this.currentLang = this.translateService.currentLang;
17+
this.currentDate = Date.now();
1618
}
1719
}

src/app/core/nav/nav.component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, Inject} from '@angular/core';
1+
import {Component, Inject, OnInit} from '@angular/core';
22
import {TranslateService} from '@ngx-translate/core';
33

44
import {APP_CONFIG, AppConfig} from '../../config/app.config';
@@ -11,7 +11,8 @@ import {ProgressBarService} from '../progress-bar.service';
1111
styleUrls: ['./nav.component.scss']
1212
})
1313

14-
export class NavComponent {
14+
export class NavComponent implements OnInit {
15+
1516
appConfig: any;
1617
menuItems: any[];
1718
progressBarMode: string;
@@ -21,9 +22,11 @@ export class NavComponent {
2122
private progressBarService: ProgressBarService,
2223
private translateService: TranslateService) {
2324
this.appConfig = appConfig;
24-
this.loadMenus();
2525
this.currentLang = this.translateService.currentLang;
26+
}
2627

28+
ngOnInit() {
29+
this.loadMenus();
2730
this.progressBarService.updateProgressBar$.subscribe((mode: string) => {
2831
this.progressBarMode = mode;
2932
});

src/app/core/search-bar/search-bar.component.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component} from '@angular/core';
1+
import {Component, OnInit} from '@angular/core';
22
import {LoggerService} from '../logger.service';
33
import {Hero} from '../../heroes/shared/hero.model';
44
import {FormControl} from '@angular/forms';
@@ -15,16 +15,19 @@ import {AppConfig} from '../../config/app.config';
1515
]
1616
})
1717

18-
export class SearchBarComponent {
19-
defaultHeroes: Array<Hero> = [];
18+
export class SearchBarComponent implements OnInit {
19+
20+
defaultHeroes: Array<Hero>;
2021
heroFormControl: FormControl;
2122
filteredHeroes: any;
22-
heroesAutocomplete: any;
2323

2424
constructor(private heroService: HeroService,
2525
private router: Router) {
26+
this.defaultHeroes = [];
2627
this.heroFormControl = new FormControl();
28+
}
2729

30+
ngOnInit() {
2831
this.heroService.getAllHeroes().subscribe((heroes: Array<Hero>) => {
2932
this.defaultHeroes = heroes.filter(hero => hero['default']);
3033

src/app/heroes/hero-detail/hero-detail.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component} from '@angular/core';
1+
import {Component, OnInit} from '@angular/core';
22
import {Hero} from '../shared/hero.model';
33
import {HeroService} from '../shared/hero.service';
44
import {ActivatedRoute} from '@angular/router';
@@ -9,12 +9,16 @@ import {ActivatedRoute} from '@angular/router';
99
styleUrls: ['./hero-detail.component.scss']
1010
})
1111

12-
export class HeroDetailComponent {
12+
export class HeroDetailComponent implements OnInit {
13+
1314
hero: Hero;
1415
canVote: boolean;
1516

1617
constructor(private heroService: HeroService,
1718
private activatedRoute: ActivatedRoute) {
19+
}
20+
21+
ngOnInit() {
1822
this.activatedRoute.params.subscribe((params: any) => {
1923
if (params['id']) {
2024
this.heroService.getHeroById(params['id']).subscribe((hero: Hero) => {

src/app/heroes/hero-list/hero-list.component.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ViewChild} from '@angular/core';
1+
import {Component, OnInit, ViewChild} from '@angular/core';
22
import {Hero} from '../shared/hero.model';
33
import {HeroService} from '../shared/hero.service';
44
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
@@ -23,7 +23,8 @@ export class RemoveHeroDialogComponent {
2323
styleUrls: ['./hero-list.component.scss']
2424
})
2525

26-
export class HeroListComponent {
26+
export class HeroListComponent implements OnInit {
27+
2728
heroes: Hero[];
2829
newHeroForm: FormGroup;
2930
canVote = false;
@@ -40,7 +41,9 @@ export class HeroListComponent {
4041
'name': ['', [Validators.required]],
4142
'alterEgo': ['', [Validators.required]]
4243
});
44+
}
4345

46+
ngOnInit() {
4447
this.heroService.getAllHeroes().subscribe((heroes: Array<Hero>) => {
4548
this.heroes = heroes.sort((a, b) => {
4649
return b.likes - a.likes;

src/app/heroes/hero-top/hero-top.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component} from '@angular/core';
1+
import {Component, OnInit} from '@angular/core';
22
import {Hero} from '../shared/hero.model';
33
import {HeroService} from '../shared/hero.service';
44
import {AppConfig} from '../../config/app.config';
@@ -9,15 +9,17 @@ import {Router} from '@angular/router';
99
templateUrl: './hero-top.component.html',
1010
styleUrls: ['./hero-top.component.scss']
1111
})
12-
export class HeroTopComponent {
12+
export class HeroTopComponent implements OnInit {
1313

1414
heroes: Hero[] = null;
1515
canVote = false;
1616

1717
constructor(private heroService: HeroService,
1818
private router: Router) {
1919
this.canVote = this.heroService.checkIfUserCanVote();
20+
}
2021

22+
ngOnInit() {
2123
this.heroService.getAllHeroes().subscribe((heroes) => {
2224
this.heroes = heroes.sort((a, b) => {
2325
return b.likes - a.likes;

0 commit comments

Comments
 (0)