How to set values in a component model before opening a dialog through a service [Angular 4] -
i have subjectdialogcomponent
child of subjectpopupcompoenet
, both of them communicate via subjectpopupservice
. want change model of subjectdialogcomponent
before gets opened, specially have select
components , want change model of select component based on conditions using subjectpopupservice
. have now.
@component({ selector: 'subject-dialog', templateurl: `<div class="form-group"> <label for="field_project">project</label> <select class="form-control" id="field_project" name="project" [(ngmodel)]="subject.project" required> <option [ngvalue]="null"></option> <option [ngvalue]="projectoption.id === subject.project?.id ? subject.project : projectoption" *ngfor="let projectoption of projects; trackby: trackprojectbyid">{{projectoption.projectname}}</option> </select> </div>`' }) export class subjectdialogcomponent implements oninit { subject: subject; // can changed issaving: boolean; projects: project[]; // want change values of projects ngoninit() { if(this.projects==null) { this.projectservice.query().subscribe( (res) => { this.projects = res.json(); }); } } // other methods }
my subjectpopupcompoenet follows. trying change model based on whether have subject, project specific request.
@component({ selector: 'subject-popup', template: '' }) export class subjectpopupcomponent implements oninit, ondestroy { modalref: ngbmodalref; routesub: any; constructor(private route: activatedroute, private router : router, private subjectpopupservice: subjectpopupservice, ) { } ngoninit() { this.router.routerstate.root.firstchild.url.subscribe(url => { if(url[0].path === 'project' && url[1].path) { this.routesub = this.route.params.subscribe((params) => { if (params['id']) { this.modalref = this.subjectpopupservice .open(subjectdialogcomponent, params['id'] , url[1].path); } else { this.modalref = this.subjectpopupservice .open(subjectdialogcomponent , null , url[1].path); } }); } else { this.routesub = this.route.params.subscribe((params) => { if (params['id']) { this.modalref = this.subjectpopupservice .open(subjectdialogcomponent, params['id']); } else { this.modalref = this.subjectpopupservice .open(subjectdialogcomponent); } }); } }); } ngondestroy() { this.routesub.unsubscribe(); } }
this relevant , confusing part. in service
alter subject
model , reflect correctly in ui. not projects
. get loaded in ngoninit
method.
@injectable() export class subjectpopupservice { private isopen = false; constructor( private modalservice: ngbmodal, private router: router, private subjectservice: subjectservice, private projectservice: projectservice, ) {} open(component: component, id?: number | , projectid?: number | any): ngbmodalref { if (this.isopen) { return; } this.isopen = true; if (id) { this.subjectservice.find(id).subscribe((subject) => { this.subjectmodalref(component, subject , projectid); }); } else { var subject = new subject(); return this.subjectmodalref(component, subject, projectid); } } subjectmodalref(component: component, subjects: subject , projectid?: number ): ngbmodalref { const modalref = this.modalservice.open(component, { size: 'lg', backdrop: 'static'}); modalref.componentinstance.subject = subjects; // set subject if(projectid) { this.projectservice.find(projectid).subscribe((project) => { let projects = [project]; modalref.componentinstance.projects = projects; // not set here in select component console.log('here',modalref.componentinstance.projects) }); } return modalref; } }
so think when modelref.open
instance created executes ngoninit
, don't know how change in there or set value before open dialog.
can body advice me on how solve this? please kind of advice appreciated , thanking in advance.
Comments
Post a Comment