简体   繁体   English

与服务,component.ts和component.html的通信不起作用

[英]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;
        });
    }
}

I temporarily resolve this whit that 我暂时解决了这个问题

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.

相关问题 我的 component.html 文件没有读取我的 component.ts 变量,有什么解决办法吗? - My component.html file doesn't read my component.ts variables, any solution? 将输入从component.html发送到component.ts - Send input from component.html to component.ts component.ts中的变量不出现在component.html angular2中 - variable in component.ts not appearing in component.html angular2 ngx-translate 在 component.ts 中不起作用 - ngx-translate doesn't work in component.ts Angular应用程序的component.html中未提取来自component.ts的数据 - Data from component.ts is not fetched in component.html in angular application 加载与component.ts文件不同的component.html文件? - Loading a different component.html file from a component.ts file? Angular 依赖项最佳实践 - 是否可以在 component.html 中引用 ts 文件而不是其 component.ts? - Angular dependencies best practice - is it OK to reference ts file other than its component.ts in a component.html? 我的service.ts不会将数据返回到component.ts - My service.ts doesn't return data to component.ts 如何将 HTML 中的 {{value}} 传递给 component.ts - How to pass {{value}} in HTML to component.ts 如何从另一个 Component.ts 访问一个 Component.ts(在同一个 app.component.html 中) - How to access a Component.ts from another Component.ts (in the same app.component.html)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM