observable - Angular 2 - Access subscribe value -
how access this.existingusers in formbuilder? console.log(value) doesn't show in console. console.log(this.id) returns correct parameter.
export class usercomponent implements oninit { existinguser: = {}; ngoninit() { this.activatedroute.params.subscribe((params: params) => { this.id = params['id']; console.log(this.id); this.db.object(`/users/${this.id}`).map(value => { console.log(value); this.existinguser = value; }) }); console.log(this.existinguser); this.userform = this.formbuilder.group({ first_name: [this.existinguser.first_name, validators.required], }) }; }
i'm not clear on trying ... need code within .map method. this:
export class usercomponent implements oninit { existinguser: = {}; ngoninit() { this.activatedroute.params.subscribe((params: params) => { this.id = params['id']; console.log(this.id); this.db.object(`/users/${this.id}`).map(value => { console.log(value); this.existinguser = value; console.log(this.existinguser); this.userform = this.formbuilder.group({ first_name: [this.existinguser.first_name, validators.required], }) }) }); }; }
or put code in method called here.
update: here example application similar:
ngoninit(): void { this.productform = this.fb.group({ productname: ['', [validators.required, validators.minlength(3), validators.maxlength(50)]], productcode: ['', validators.required], starrating: ['', numbervalidators.range(1, 5)], tags: this.fb.array([]), description: '' }); // read product id route parameter this.route.params.subscribe( params => { let id = +params['id']; this.getproduct(id); } ); } getproduct(id: number): void { this.productservice.getproduct(id) .subscribe( (product: iproduct) => this.onproductretrieved(product), (error: any) => this.errormessage = <any>error ); } onproductretrieved(product: iproduct): void { if (this.productform) { this.productform.reset(); } this.product = product; if (this.product.id === 0) { this.pagetitle = 'add product'; } else { this.pagetitle = `edit product: ${this.product.productname}`; } // update data on form this.productform.patchvalue({ productname: this.product.productname, productcode: this.product.productcode, starrating: this.product.starrating, description: this.product.description }); this.productform.setcontrol('tags', this.fb.array(this.product.tags || [])); } you can find full set of code here: https://github.com/deborahk/angular2-reactiveforms (in apm folder)
Comments
Post a Comment