Skip to content

Commit

Permalink
feat(oninit): moved logic parts from constructors to ngOnInit method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismaestro authored and Ismael Ramos committed Apr 18, 2018
1 parent fc2f15f commit 329db74
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
11 changes: 6 additions & 5 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Component} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {Meta, Title} from '@angular/platform-browser';

import {NavigationEnd, Router} from '@angular/router';
import {AppConfig} from './config/app.config';
import {MatSnackBar} from '@angular/material';
Expand All @@ -13,17 +12,19 @@ declare const Modernizr;
templateUrl: './app.component.html'
})

export class AppComponent {
export class AppComponent implements OnInit {

isOnline = navigator.onLine;
isOnline: boolean;

constructor(private translateService: TranslateService,
private title: Title,
private meta: Meta,
private snackBar: MatSnackBar,
private router: Router) {
this.isOnline = navigator.onLine;
}

this.translateService = translateService;
ngOnInit() {
this.translateService.setDefaultLang('en');
this.translateService.use('en');

Expand Down
4 changes: 3 additions & 1 deletion src/app/core/footer/footer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {TranslateService} from '@ngx-translate/core';
})

export class FooterComponent {

currentLang: string;
currentDate = Date.now();
currentDate: number;

constructor(private translateService: TranslateService) {
this.currentLang = this.translateService.currentLang;
this.currentDate = Date.now();
}
}
9 changes: 6 additions & 3 deletions src/app/core/nav/nav.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, Inject} from '@angular/core';
import {Component, Inject, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';

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

export class NavComponent {
export class NavComponent implements OnInit {

appConfig: any;
menuItems: any[];
progressBarMode: string;
Expand All @@ -21,9 +22,11 @@ export class NavComponent {
private progressBarService: ProgressBarService,
private translateService: TranslateService) {
this.appConfig = appConfig;
this.loadMenus();
this.currentLang = this.translateService.currentLang;
}

ngOnInit() {
this.loadMenus();
this.progressBarService.updateProgressBar$.subscribe((mode: string) => {
this.progressBarMode = mode;
});
Expand Down
11 changes: 7 additions & 4 deletions src/app/core/search-bar/search-bar.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {LoggerService} from '../logger.service';
import {Hero} from '../../heroes/shared/hero.model';
import {FormControl} from '@angular/forms';
Expand All @@ -15,16 +15,19 @@ import {AppConfig} from '../../config/app.config';
]
})

export class SearchBarComponent {
defaultHeroes: Array<Hero> = [];
export class SearchBarComponent implements OnInit {

defaultHeroes: Array<Hero>;
heroFormControl: FormControl;
filteredHeroes: any;
heroesAutocomplete: any;

constructor(private heroService: HeroService,
private router: Router) {
this.defaultHeroes = [];
this.heroFormControl = new FormControl();
}

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

Expand Down
8 changes: 6 additions & 2 deletions src/app/heroes/hero-detail/hero-detail.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {Hero} from '../shared/hero.model';
import {HeroService} from '../shared/hero.service';
import {ActivatedRoute} from '@angular/router';
Expand All @@ -9,12 +9,16 @@ import {ActivatedRoute} from '@angular/router';
styleUrls: ['./hero-detail.component.scss']
})

export class HeroDetailComponent {
export class HeroDetailComponent implements OnInit {

hero: Hero;
canVote: boolean;

constructor(private heroService: HeroService,
private activatedRoute: ActivatedRoute) {
}

ngOnInit() {
this.activatedRoute.params.subscribe((params: any) => {
if (params['id']) {
this.heroService.getHeroById(params['id']).subscribe((hero: Hero) => {
Expand Down
7 changes: 5 additions & 2 deletions src/app/heroes/hero-list/hero-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, ViewChild} from '@angular/core';
import {Component, OnInit, ViewChild} from '@angular/core';
import {Hero} from '../shared/hero.model';
import {HeroService} from '../shared/hero.service';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
Expand All @@ -23,7 +23,8 @@ export class RemoveHeroDialogComponent {
styleUrls: ['./hero-list.component.scss']
})

export class HeroListComponent {
export class HeroListComponent implements OnInit {

heroes: Hero[];
newHeroForm: FormGroup;
canVote = false;
Expand All @@ -40,7 +41,9 @@ export class HeroListComponent {
'name': ['', [Validators.required]],
'alterEgo': ['', [Validators.required]]
});
}

ngOnInit() {
this.heroService.getAllHeroes().subscribe((heroes: Array<Hero>) => {
this.heroes = heroes.sort((a, b) => {
return b.likes - a.likes;
Expand Down
6 changes: 4 additions & 2 deletions src/app/heroes/hero-top/hero-top.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {Hero} from '../shared/hero.model';
import {HeroService} from '../shared/hero.service';
import {AppConfig} from '../../config/app.config';
Expand All @@ -9,15 +9,17 @@ import {Router} from '@angular/router';
templateUrl: './hero-top.component.html',
styleUrls: ['./hero-top.component.scss']
})
export class HeroTopComponent {
export class HeroTopComponent implements OnInit {

heroes: Hero[] = null;
canVote = false;

constructor(private heroService: HeroService,
private router: Router) {
this.canVote = this.heroService.checkIfUserCanVote();
}

ngOnInit() {
this.heroService.getAllHeroes().subscribe((heroes) => {
this.heroes = heroes.sort((a, b) => {
return b.likes - a.likes;
Expand Down

0 comments on commit 329db74

Please sign in to comment.