首页 > 解决方案 > 如何在角度服务中调用 web api

问题描述

我是角度的新手。有人可以帮我知道如何用角码调用我的 api 吗?

我的 We API 是 -

[HttpGet,Route("api/customer/getCustomer/{name}")]
public async Task<IHttpActionResult> getCustomer([FromUri] string name) {
...}

在anular中,我想像下面这样打电话 -

customer.component.ts -

name = "test";
getCustomer(){
this.custService.getCustomer(this.name).subscribe((data) => {
this.cust = JSON.parse(data.toString());
});
}

custService 代码 -

getCustomer(name){
 return this.http.get('https://localhost:44400/api/customer/getCustomer/', +name)
}

标签: c#angularasp.net-web-api

解决方案


客户.component.ts

export class CustomerComponent implements OnInit {
name: string;
cust: any;
constructor(private custService: CustService){
}
ngOnInit(){
    this.name = "test";
    this.getCustomer();
}

getCustomer(){
    this.custService.getCustomer(this.name).subscribe((data) => {
    this.cust = data;
    },error => {
        console.log(error);
    }
    );
}

推荐阅读