[英]Convert JavaScript Object to string
I'm working on a project in Angular 8 in which I post values from a textArea and get an Object as the following from an API: API response , I'm trying to access "raisonSociale" but I can't find a way to做
這是我的 http.post:
postResponse: any;
postRefs(){
return this.httpClient.post(this.url,this.datas)
.subscribe(
data => {
console.log(data)
const parsed = JSON.stringify(data)
return this.postResponse = parsed;
},
);
}
因此,我將來自 POST 的響應存儲在 postReponse 中,並嘗試在我的 html 文件中呈現
<tr>
<td *ngIf="postResponse">{{postResponse.crossPart.all}}</td>
<td *ngIf="postResponse">{{postResponse}}</td>
<td *ngIf="postResponse">{{postResponse}}</td>
<td *ngIf="postResponse">{{postResponse}}</td>
</tr>
但在我的 HTML 它呈現如下: HTML 結果
我試過的:
我嘗試不使用 const parsed:
postRefs(){
return this.httpClient.post(this.url,this.datas)
.subscribe(
data => {
console.log(data)
JSON.stringify(data)
return this.postResponse = data;
},
);
}
以上返回我 [Object Object]: Object 結果
提前感謝您的時間和幫助。
編輯:
謝謝大家的回答!
@DmytroHuz 解決方案使它像魅力一樣工作,這是需要做的:
<tr *ngFor="let item of postResponse.crossPart.found">
<td *ngIf="postResponse">{{item.articleSociete}}</td>
<td *ngIf="postResponse">{{item.raisonSociale}}</td>
<td *ngIf="postResponse">{{postResponse}}</td>
<td *ngIf="postResponse">{{postResponse}}</td>
</tr>
您正在嘗試在您放入postResponse
變量的 HTML object 中進行設置。 如果您需要從響應中獲取特定值,則需要以如下方式調用它: postResponse.crossPart.all[0].raisonSociale
。 或者您可以將*ngFor
用於postResponse.crossPart.all
<tr *ngFor="let item of postResponse.crossPart.all>
<td *ngIf="postResponse">{{item.raisonSociale}</td>
...
</tr>
postResponse 是 object,因此當您在 HTML 中訪問它時,它會打印為 object。
postResponse.crossPart.all 是一個數組,因此它再次打印為 object。
指定 object 屬性或在數組的情況下循環以打印原始值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.