How to move a
Variable from constructor call to a local method
Task:
I want to
move the java Typescript code of the Angular constructor to a local ngOnInit()
The code
should be moved from constructor to ngOnInit()
Constructor
constructor(afDb:AngularFireDatabase) {
//*works
const itemsRef: AngularFireList<any> = afDb.list('Aufgaben');
itemsRef.valueChanges().subscribe(
x=>{ this.firebase_Data = x;}
);
}
|
Man kann den javascript Code verschieben indem man die include
parameter bei aufruf des constructors mit private declariert und beim Aufruf in
den neuen Funktion ein this. Hinzufügt
Code after the changeover.
With .this in
the method and private in the constructor
constructor(private afDb:AngularFireDatabase) {
}
ngOnInit()
{
//*works
const itemsRef: AngularFireList<any> = this.afDb.list('Aufgaben');
itemsRef.valueChanges().subscribe(
x=>{ this.firebase_Data = x;}
);
}
|
App.component.ts
vor der Umstellung
export class AppComponent {
//--< Daten >--
title = 'dailycheck';
//public firebase_Data: Observable<any>[]; //works with AngularFireList<any>..subscribe(x=>{ this.aufgaben= x;}
public firebase_Data;
//--</ Daten >--
//--< Daten von Firebase holen >--
constructor(afDb:AngularFireDatabase) {
//*works
const itemsRef: AngularFireList<any> = afDb.list('Aufgaben');
itemsRef.valueChanges().subscribe(
x=>{ this.firebase_Data = x;}
);
}
ngOnInit()
{
}
//--</ Daten von Firebase holen >--
}
|
App.component.ts nach der Umstellung
import { Component } from '@angular/core';
import { AngularFireDatabase,AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
//--< Daten >--
title = 'dailycheck';
//public firebase_Data: Observable<any>[]; //works with AngularFireList<any>..subscribe(x=>{ this.aufgaben= x;}
public firebase_Data;
//--</ Daten >--
//--< Daten von Firebase holen >--
constructor(private afDb:AngularFireDatabase) {
}
ngOnInit()
{
//*works
const itemsRef: AngularFireList<any> = this.afDb.list('Aufgaben');
itemsRef.valueChanges().subscribe(
x=>{ this.firebase_Data = x;}
);
}
//--</ Daten von Firebase holen >--
}
|