如何将选择标记选项值附加到Web服务角度4

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将选择标记选项值附加到Web服务角度4相关的知识,希望对你有一定的参考价值。

我有一个Web服务来发布一些数据来创建配置文件,

所以我有一个表格的下拉列表

当我尝试从表单上的所有内容发布数据时,它工作正常

但是下拉菜单中所选选项的选项值不会发布

它给了我'未定义'的错误

我问后端团队,他们告诉我,我的结果是我没有绑定JSON数组中的正确数据

我需要知道发布期权价值的正确方法是什么

如果我在下拉列表中选择了多个选项,比如多选下拉菜单,如何在数组中发布所有选项?

这是我的组件html

<div class="row col-md-12" style="margin-bottom: 15px; text-align: 
center">
            <select name="country" class="rounded-inputs20 select-
select col-md-3" (change)="onChangeCountry($event.target.value)">
              <option *ngFor="let country of countries"  
[value]="country.id">{{country.name}}</option>
            </select>
            <select name="city" class="rounded-inputs20 select-select 
col-md-3"  (change)="onChangeCity($event.target.value)">
              <option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</option>
            </select>
            <select name="districts"  class="rounded-inputs20 select-
select col-md-3">
              <option *ngFor="let district of districts" 
[value]="district.id">{{district.name}}</option>
            </select>
          </div>

我的组件ts:

name: any[];
logo: any[];
vision: any[];
mission: any[];
address: any[];
emails: any[] = [];
numbers: any[] = [];
school_id: any[];
district_id: any[];
// latitude: any[];
// longitude: any[];
numbertitle: number;
number: number;
private url = 
'https://crm.easyschools.org/api/en/schools/create/create';
 countries: any[] = []; 
 cities: any[] = []; 
 districts: any[] = []; 
imageFile: any;
schoolyears: any[] = [];
start_date: string;
end_date: string;
name_en: any[] = [];
id: number[] = [];
type: any[] = [];


onChangeCountry(countryId: number) {
this.cities = this.countries.filter(x => x.id == countryId)[0].cities;
this.districts = this.cities.filter(x => x.id == this.cities[0].id)
[0].districts;
 }

onChangeCity(cityId: number) {
this.districts = this.cities.filter(x => x.id == cityId)[0].districts;
 }


private getMyBlog() {
return this._http.get('http://crm.easyschools.org/api/en/regions')
// .map((res) => res.json())
  .subscribe(countries => {
    this.countries = countries.data;
    console.log(countries);
    this.cities = this.countries.filter(x => x.id === 1)[0].cities;
    this.districts = this.cities.filter(x => x.id === 1)[0].districts;
  });
 }
 getFile(event){
 this.imageFile =  event.target.files[0];
 }
 onSubmit(form: NgForm) {
 var data = form.value;
 let formData: FormData = new FormData();
 // debugger;

 formData.append('logo', this.imageFile, this.imageFile.name);
 formData.append('name', data.name1);
 formData.append('vision', data.vision);
 formData.append('mission', data.mission);
 formData.append('address', data.address);
 formData.append('latitude', 1);
 formData.append('longitude', 1);
 formData.append('district_id', data.districts);
 formData.append('emails[0][title]', data.title);
 formData.append('emails[0][email]', data.email);
 formData.append('numbers[0][title]', data.numbertitle);
 formData.append('numbers[0][number]', data.number);
 formData.append('schoolyears[0][start_date]', data.start_date);
 formData.append('schoolyears[0][end_date]', data.end_date );
 formData.append( 'schooltypes[0][type_id]', 1 );

 this._http.post(this.url, formData)
  .subscribe(response => {
    // debugger;
    console.log(response);
  }, (err: HttpErrorResponse) => {
    console.log(err);
  });

 }

当我在邮递员上尝试网络服务API并给它一个数字(1)它给我scode 200,问题是HTML中的输入正确读取值

如果缺少任何内容或者您需要更多详细信息,只需发表评论,我将为您提供所需的所有数据,我需要知道zone_id部分有什么问题

随意尝试邮递员的API

答案

解决方案1(多个值)

component.ts

district_id: number[] = [];
...
...
...
onSubmit(form: NgForm) {
    ...
    `formData.append('district_id', this.district_id);`
    ...
}

component.html

...
<select multiple [(ngModel)]="district_id" name="districts" class="rounded-inputs20 select-select col-md-3">
        <option *ngFor="let district of districts" [value]="district.id">{{district.name}}</option>
</select>
...

解决方案2:

component.ts

district_id: number;
...
...
...
onSubmit(form: NgForm) {
    ...
    `formData.append('district_id', this.district_id);`
    ...
}

component.html

...
<select [(ngModel)]="district_id" name="districts" class="rounded-inputs20 select-select col-md-3">
        <option *ngFor="let district of districts" [ngValue]="district.id">{{district.name}}</option>
</select>
...
另一答案

您可以按如下方式更新更改回调:

onChangeCity($event.target.options[$event.target.selectedIndex].value)

以上是关于如何将选择标记选项值附加到Web服务角度4的主要内容,如果未能解决你的问题,请参考以下文章

如何使用CasperJS将选项值插入到选择标记中?

角度将组件附加到各个选项卡

将片段附加到framelayout时如何删除空格

重新附加时片段创建/恢复重复视图

片段未附加到我的活动

java 简单的代码片段,展示如何将javaagent附加到运行JVM进程