4 回答

TA貢獻1801條經(jīng)驗 獲得超16個贊
如果要直接查看響應(yīng)數(shù)據(jù)而不設(shè)置任何格式。
只需使用 JSON 管道{{ responseData | json }}
您需要為控制器中的任何變量分配響應(yīng)
responseData: any;
submitForm() {
var formData: any = new FormData();
formData.append("inputText", this.form.get('inputText').value)
this.http.post('http://127.0.0.1:5000/predictSentiment', formData).subscribe(
(response) => { this.responseData = response }, // <-- assign the value here
(error) => { console.log(error) }
);
}
然后在模板中使用 JSONpipe 綁定變量
<div class="card col-md-6">
<div class="card-body">
<h2 class="card-title">Output:</h2>
<div>
{{responseData | json }}
</div>
</div>
</div

TA貢獻1735條經(jīng)驗 獲得超5個贊
根據(jù)經(jīng)驗,盡量不要從 模板表達(dá)式 (如 、 和 ) 調(diào)用函數(shù)。它們通常被調(diào)用的次數(shù)比看起來的次數(shù)要多。*ngFor="let data of submitForm()*ngIf="submitForm()"{{ submitForm() }}
當(dāng)然,我們可以通過更改檢測策略來解決此行為,但這將是解決問題的解決方案,否則可以輕松解決。
在您的例子中,您可以在局部變量中分配HTTP響應(yīng),并在模板中循環(huán)訪問它。請嘗試以下操作
控制器
form: FormGroup;
responseData: any = []; // <-- define a variable and assign an empty array
constructor(
public fb: FormBuilder,
private http: HttpClient
) {
this.form = this.fb.group({
inputText: ['']
})
}
ngOnInit() {}
submitForm() {
var formData: any = new FormData();
formData.append("inputText", this.form.get('inputText').value)
this.http.post('http://127.0.0.1:5000/predictSentiment', formData).subscribe(
(response) => { this.responseData = response }, // <-- assign the value here
(error) => { console.log(error) }
);
}
模板
<ng-container *ngIf="responseData.length > 1">
<div class="card col-md-6">
<div class="card-body">
<h2 class="card-title">Output:</h2>
<div *ngFor="let data of responseData">
{{data?.response}}
</div>
</div>
</div>
</ng-container>

TA貢獻1810條經(jīng)驗 獲得超5個贊
您正在 *ngFor 中調(diào)用方法 submitForm(),但它沒有返回任何內(nèi)容。相反,您可以聲明一個變量并分配 repsonse
data : any;
this.http.post('http://127.0.0.1:5000/predictSentiment', formData).subscribe((response) =>
this.data = response,
(error) => console.log(error)
)
并將您的 HTML 更改為
<div class="card-body">
<h2 class="card-title">Output:</h2>
<div *ngFor="let data of data">
{{data.response}}
</div>
</div>

TA貢獻1824條經(jīng)驗 獲得超8個贊
您可以有一個變量,您可以在其中設(shè)置收到的響應(yīng)。
form: FormGroup;
result: any[];
constructor(
public fb: FormBuilder,
private http: HttpClient
) {
this.form = this.fb.group({
inputText: ['']
})
}
ngOnInit() {}
submitForm() {
var formData: any = new FormData();
formData.append("inputText", this.form.get('inputText').value)
this.http.post('http://127.0.0.1:5000/predictSentiment', formData).subscribe(
(response) => { this.result = response; },
(error) => console.log(error)
)
}
在 html 中綁定變量以顯示內(nèi)容:
<div class="card col-md-6">
<div class="card-body">
<h2 class="card-title">Output:</h2>
<div *ngFor="let data of results">
{{data.id}} - {{data.name}}
</div>
</div>
</div>
示例:如果返回的數(shù)據(jù)如下所示:
[
{ id: 1, name: "XXX"},
{ id: 2, name: "YYY"},
]
輸出為:
1 - XXX
2 - YYY
添加回答
舉報