问题描述:

I am trying to pass some value from component to service constructor.

my object that trying to pass param.

My code is:

 constructor(public af: AngularFire, public userData: UserData ,param: boolean) {

this.smartTableData = this.af.database.list('events/' + this.userData.user.uid + '/contacts',{

preserveSnapshot: param, //this param should be taken from component

query: {

orderByChild: 'tableNumber'

}

});

and my component:

import {LocalDataSource} from 'ng2-smart-table';

@Component({

selector: 'basic-tables',

encapsulation: ViewEncapsulation.None,

styles: [require('./smartTables.scss')],

template: require('./smartTables.html')

})

export class SmartTables {

constructor(protected service: SmartTablesService) {

this.service.smartTableData.subscribe((data) => {

console.log(data);

this.source.load(data);

});

}

网友答案:

It isn't convenient to do a request in service constructor, because service instance is singleton within current injector. af.database.list is called only once on the first injection, and param would be injected only once too.

Another problem is that DI happens before component constructor.

It should be

constructor(public af: AngularFire, public userData: UserData) {}

getData(param: boolean) {
    return this.af.database.list(...);
}
相关阅读:
Top