[英]Angular get value from NgModel
I want to send multiple values from a to an API function.我想将多个值从 a 发送到 API function。
on the DB I store the values in text format.在数据库上,我以文本格式存储值。 The value that is stored is array
存储的值是数组
on the Swal alert, I am getting [object object] but I want to get each value eg Painting or Graphic Design.在 Swal 警报中,我得到 [object object] 但我想获得每个值,例如绘画或图形设计。
Here is my code so far.到目前为止,这是我的代码。
HTML 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 .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()
}
user-data.ts用户数据.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 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);
}
First of all, when naming object properties in JS, the usual naming convention is camelCase.首先,在JS中命名object属性时,通常的命名约定是camelCase。 For example:
例如:
creative.Painting
should become creative.painting
creative.Painting
应该变成creative.painting
creative.Graphic Design
should become creative.graphicDesign
creative.Graphic Design
应该变成creative.graphicDesign
Secondly, you are passing the whole creative
object to Swal and it expects a string, that is why you get [object Object]
.其次,您将整个
creative
object 传递给 Swal,它需要一个字符串,这就是您得到[object Object]
的原因。 It can't automatically assume which property to display, you need to explicitly state that.它不能自动假定要显示哪个属性,您需要明确 state 。 One solution would be to pass the title you would like to display as an argument of the
creativeInterest(creative:string)
method, ie:一种解决方案是将您想要显示的标题作为
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()
}
and in you component markup (unchanged parts omitted from the snippet below):并在您的组件标记中(下面的代码段中省略了未更改的部分):
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.