[英]Communication with the service, the component.ts and the component.html doesn't work
@Injectable({
providedIn: 'root'
})
export class PatientService {
listpatient;
constructor(private httpClient:HttpClient) {
this.httpClient.get("http://localhost:8010/patients").subscribe(data=>{this.listpatient=data;},err=>{console.log(err);})
}
}
---------------------------------------
@Component({
selector: 'app-pat',
templateUrl: './patient.component.html',
styleUrls: ['./patient.component.css'],
})
export class PatientComponent implements OnInit {
@Input()listpatient;
constructor(patient:PatientService){
this.listpatient=patient.listpatient;
}
ngOnInit() {}
}
---------------------------------------
<table *ngIf="listpatient" class="table">//he don't enter in the table
<tr>
<th>ID</th><th>Nom</th><th>Prenom</th> <th></th>
</tr>
<tr *ngFor="let f of listpatient">
<td>{{f.id}}</td>
<td>{{f.nom}}</td>
<td>{{f.prenom}}</td>
<td routerLink="patientdetail" routerLinkActive="active" type="button" class="btn btn-light">Obtenir les infos</td>
</tr>
</table>
I load my list of patients from spring, but I have a problem it does not show my list, I think I can not emit the data to the html. 我从春天加载我的患者列表,但我有一个问题它没有显示我的列表,我想我不能将数据发送到html。 It doesn't enter in the
*ngIf
它不会输入
*ngIf
In your service you should have a method and it should be returning the call to the db 在您的服务中,您应该有一个方法,它应该将调用返回到数据库
patientlist() {
return this.httpClient.get("http://localhost:8010/patients")
}
Also, I believe you should be subscribing in the component rather than the service 此外,我相信你应该订阅组件而不是服务
this.patient.patientlist.subscribe(data=>{this.listpatient=data;},err=>{console.log(err);})
Create a method getPatients()
inside the service class as below and call that method in the component. 在服务类中创建一个方法
getPatients()
,如下所示,并在组件中调用该方法。
Service.ts Service.ts
@Injectable({
providedIn: 'root'
})
export class PatientService {
listpatient;
constructor(private httpClient: HttpClient) { }
getPatients() {
this.httpClient.get("http://localhost:8010/patients")
.map(data => { return data; });
}
}
Component.ts Component.ts
@Component({
selector: 'app-pat',
templateUrl: './patient.component.html',
styleUrls: ['./patient.component.css'],
})
export class PatientComponent implements OnInit {
@Input() listpatient;
constructor(patient: PatientService) {
}
ngOnInit() {
this.patient.getPatients().subscribe(data => {
this.listpatient = data;
});
}
}
export class PatientComponent implements OnInit {
@Input() listpatient;
constructor(private httpClient:HttpClient){
}
ngOnInit() {
this.httpClient.get("http://localhost:8010/patients").subscribe(data=>{this.listpatient=data})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.