我想將多個值從 a 發送到 API 函式。
在資料庫上,我以文本格式存盤值。存盤的值是陣列
在 Swal 警報中,我得到 [object object] 但我想獲得每個值,例如繪畫或圖形設計。
到目前為止,這是我的代碼。
HTML
<ion-item>
<ion-label>Painting</ion-label>
<ion-toggle color="gold" [(ngModel)]="creative.Painting" (click)="creativeInterest(creative)"></ion-toggle>
</ion-item>
<ion-item>
<ion-label>Graphic Design</ion-label>
<ion-toggle color="tertiary" [(ngModel)]="creative.Graphic Design" (click)="creativeInterest(creative)"></ion-toggle>
</ion-item>
.ts
export class CreativeSettingsPage implements OnInit {
creative: any = {};
userDetails: any = {};
constructor(
public userData: UserData
) {
this.userDetails = this.userData.getUserData();
}
ngOnInit() {
}
creativeInterest(creative:string)
{
this.userData.creativeSettings(this.userDetails.uid, creative).pipe(
map((data: any) => {
if (data.success) {
Swal.fire({
icon: 'success',
title: creative,
showConfirmButton: false,
backdrop: false,
timer: 2500
})
}
})
).subscribe()
}
用戶資料.ts
creativeSettings(uid: number, creative:any) {
const url = this.appData.getApiUrl() 'creativeSettings';
const data = this.jsonToURLEncoded({
uid: uid,
creative: creative
});
return this.http.post(url, data, { headers: this.options });
}
PHP
function creativeSettings()
{
$request = \Slim\Slim::getInstance()->request();
$response['success'] = true; // 1 true if not errors OK NOTHING
$uid = $request->post('uid');
$creative_interests = $request->post('creative');
$db = getDB();
$sql = "UPDATE users SET creative_interests = :creative_interests WHERE uid = :uid";
$stmt = $db->prepare($sql);
$stmt->bindParam("uid", $uid);
$stmt->bindParam("creative_interests", $creative_interests);
$stmt->execute();
$db = null;
echo json_encode($response);
}
uj5u.com熱心網友回復:
首先,在 JS 中命名物件屬性時,通常的命名約定是駝峰式。例如:
creative.Painting應該成為creative.painting
creative.Graphic Design應該成為creative.graphicDesign
其次,您將整個creative物件傳遞給 Swal 并且它需要一個字串,這就是您得到[object Object]. 它不能自動假定要顯示哪個屬性,您需要明確說明。一種解決方案是將您想要顯示的標題作為creativeInterest(creative:string)方法的引數傳遞,即:
creativeInterest(creative:string, messageTitle: string)
{
this.userData.creativeSettings(this.userDetails.uid, creative).pipe(
map((data: any) => {
if (data.success) {
Swal.fire({
icon: 'success',
title: messageTitle,
showConfirmButton: false,
backdrop: false,
timer: 2500
})
}
})
).subscribe()
}
并在您的組件標記中(下面的代碼段中省略了未更改的部分):
<ion-toggle color="gold" [(ngModel)]="creative.painting" (click)="creativeInterest(creative, 'Painting')"></ion-toggle>
<ion-toggle color="tertiary" [(ngModel)]="creative.graphicDesign" (click)="creativeInterest(creative, 'Graphic Design')"></ion-toggle>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/417556.html
標籤:
上一篇:格式化物件陣列
