0
Good afternoon, I need a hand with Angular 4
I have the following method:
import { Component, OnInit, Input, Output } from '@angular/core';
import { Remedy } from '../remedy/remedy.model'
import { RemedysService } from '../remedys.service'
import { ActivatedRoute } from '@angular/router'
import { Observable } from 'rxjs/Observable'
@Component({
selector: 'gmr-remedy-details',
templateUrl: './remedy-details.component.html'
})
export class RemedyDetailsComponent implements OnInit {
remedy: Remedy
constructor(private remedysService: RemedysService, private route: ActivatedRoute) { }
ngOnInit() {
this.remedysService.remedyByMenuId(this.route.snapshot.params['id'])
.subscribe(remedy => this.remedy = remedy)
console.log(`Parametro : ${this.route.snapshot.params['id']}, Remédio ${this.remedy}`)
}
}
To be placed in a template:
<dl class="col-sm-9 col-xs-12">
<dt>Categoria</dt>
<dd>{{remedy?.des_category}}</dd>
<dt>Dosagem</dt>
<dd>{{remedy?.des_dosage}}</dd>
<dt>Descrição</dt>
<dd>{{remedy?.des_description}}</dd>
<a href="#" [routerLink]="['/remedy-register']">
<h4>Cadastrar novo remédio</h4>
</a>
</dl>
web service search service:
remedyByMenuId(id: string): Observable<Remedy> {
return this.http.get(`${GMR_API}/api/remedys/remedysMenu/${id}`)
.map(response => response.json())
.catch(ErrorHandler.handleError)
}
Note: the URL works because I use for other services and works normally.
But I can’t get Bind so that the values appear in HTML, and I check in the browser by the debug tool the object with all the values of the web service are being returned normally to something I’m doing wrong ?
you can replace
.subscribe(remedy => this.remedy = remedy)
for.subscribe(remedy => { this.remedy = remedy; console.log(this.remedy); })
and tell me what is imprinted on the console?– mercador
@merchant has a log in the init function it back Undefined pq I searched the Remedy and set before the execution of the on init then comes Undefined but Since you put it not tested I will test tomorrow and put here worth !
– Felipe Sangiorge
undefined
in thengOnInit
is expected because your request may take time to return something. Inside the callback of Success onsubscribe
should come something.– mercador
Opa good morning all right then the return was an object : log subscribe: [Object Object]
– Felipe Sangiorge
then I believe that for every reference of
remedy
in your template, if you use*ngIf
must solve.– mercador
will be ? but as I would in the template *ngIf="Remedy" ?
– Felipe Sangiorge
Let’s go continue this discussion in chat.
– mercador