ASP .net WebApiを使用して企業の一覧を取得し、単一の会社を取得するAPIを作成しました。サービスコールGetCompaniesは正常に動作し、データを取得してリストを出力します。しかし、問題はGetCompanyサービスで発生し、ログに記録すると会社になりますが、会社オブジェクトには入りません。 Angular Component and Serviceで何が間違っているのですか?どんな助けもありがとうございます。ここで何が問題なのですか?ASP .net webapi
ここにアプリケーションの出力を示します。 GetCompaniesはすべての企業をリストしますが、GetCompanyは[object Object]として出力します。 。 here is the output
Here is the screen shot of data coming from APIs.
これはcompanies.component.ts
import { Component, OnInit } from '@angular/core';
import { CompaniesService } from './companies.service';
import { Company } from './company.model';
@Component({
selector: 'app-companies',
template: `
<p>
company name = {{company}}
</p>
<ul>
<li *ngFor = "let c of companies"> {{c.Name}} - {{c.CompanyID}} </li>
</ul>
`
})
export class CompaniesComponent implements OnInit {
text: string;
errorMessage: string;
public company: Company;
public companies: Company[];
constructor(private cService: CompaniesService) { }
ngOnInit() {
this.getCompanies();
this.getCompany(5);
console.log(this.company);
}
getCompanies() {
return this.cService.getCompanies()
.subscribe(companies => this.companies = companies,
error => this.errorMessage =<any>error);
}
getCompany(id: number) {
return this.cService.getCompany(id)
.subscribe(company => this.company = company,
error => this.errorMessage =<any>error);
}
}
これはcompanies.service.tsありますcompany.model.ts
export class Company {
CompanyID: number;
Name: string;
Description: string;
EmailAddress: string;
Phone: string;
Address: string;
CreatedBy: number;
CreatedDate: Date;
UpdatedBy: number;
UpdatedDate: Date;
IsActive: boolean;
}
'{{company.Name}}'を試したことがありますか? – yurzui
はい、しました。 「未定義のName 'プロパティを読み取ることができません」 エラーTypeError:Object.eval [CompaniesResponder]として の未定義の' Name 'プロパティを読み取ることができません(CompaniesComponent.html:2) – Mamidi