angular - 如何将从下拉多选接收的数组值转换为角度5中的字符串
问题描述
我使用了一个材料下拉多选,用户可以通过它选择多个数据。当数据被发送到组件时,数据以数组格式接收,API 不接受将其插入数据库。我的查看代码:
<mat-input-container>
<input matInput type="number" formControlName="around" placeholder="Around" required >
<p class="invalid-text" *ngIf="JifForm.controls.around.invalid &&
(JifForm.controls.around.dirty || JifForm.controls.around.touched)">
<span *ngIf="JifForm.controls.around.errors.required">Around is required.</span></p>
</mat-input-container>
<mat-form-field>
<mat-select placeholder="Front Pass" formControlName="job_front_color" required multiple>
<mat-option value="pink">Pink</mat-option>
<mat-option value="black">Black</mat-option>
</mat-select>
</mat-form-field>
它以以下格式提供数据:
job_deno: 12345
job_front_color:(2) ["pink", "black"]
有没有办法让我job_front_color
以字符串格式获取值,pink, black
以便该值可以作为字符串通过 api
我通过以下代码将数据从表单传递到 api:
onSubmit(form) {
//console.log('AAAAAAAAAAAAAAAAAAAAAAAAAAAa', form);
return this._http.post('http://localhost:8000/api/v1/jobInstructionForm ', form, {headers: this.headers}).subscribe(res => {
this.flashMsg.flashMsg('success', 'added', 'JIF added');
this._router.navigate( ['pages/jif'] );
} );
}
解决方案
您可以使用Array.prototype.join()
-文档
在你的情况下,你想要的是join(', ')
let arr = ["pink", "black"]
console.log(arr.join(', '));
编辑
如果您只想将该字符串发送到您的 API,只需执行以下操作:
onSubmit(form) {
const stringOfColours = form.job_front_color.join(', );
return this._http.post('http://localhost:8000/api/v1/jobInstructionForm ', stringOfColours, {
headers: this.headers
}).subscribe(res => {
this.flashMsg.flashMsg('success', 'added', 'JIF added');
this._router.navigate(['pages/jif']);
});
}
推荐阅读
- javascript - 无法使用 $compile 编译动态 Html
- ruby-on-rails - Rails 迁移:更改列类型并更新现有数据
- python - 无法使用 ace-builds 设置 themePath
- azure-devops - 管道设置有什么作用,因为它们不影响 YAML
- c# - Xamarin.Android 中的 testDefaultGrantsWithRemoteExceptions
- docker - 本地 cassandra 实例的联系点
- facebook - Facebook Pixel - 第一方 Cookie 选项
- string - 什么模式或(循环和模式)可以在单词中找到所有可能的字符并保持在 LUA 中创建的序列
- c# - 在 C# .net MVC 中使用 ajax 下载文件
- node.js - 基于 docker-compose.yml 文件的节点环境变量的切换命令