Javascript ng select未发送字段的id

Javascript ng select未发送字段的id,javascript,html,angular,typescript,Javascript,Html,Angular,Typescript,我是Angular的新手,我有一个ng选择框,我可以从中选择国家,ng选择选项工作正常,当我提交我选择的选项时,它会提交id,但是,当我将数据绑定到ng选择并提交已经显示的数据时,它会提交名称而不是id,我想提交id,我试过下面的方法,但似乎有点不对劲。 我知道这是非常基本的,但我已经尝试了过去3个小时,感谢您的帮助 company.component.ts Country: new FormGroup({ Id: new FormControl('') }), getCountr

我是Angular的新手,我有一个ng选择框,我可以从中选择国家,ng选择选项工作正常,当我提交我选择的选项时,它会提交id,但是,当我将数据绑定到ng选择并提交已经显示的数据时,它会提交名称而不是id,我想提交id,我试过下面的方法,但似乎有点不对劲。 我知道这是非常基本的,但我已经尝试了过去3个小时,感谢您的帮助

company.component.ts

Country: new FormGroup({
    Id: new FormControl('')
  }),

getCountries(){
this.restService.GetAllCountries().subscribe((res: any) => {
  this.countries = res.data.lookups
  })
}

getCompany(){
  this.restService.getCompanyData(this.role).subscribe((res: any) => {
  this.countryLookupId = this.company.country.name_FL;
  //this returns with a string
})
}
company.component.html

<ng-select *ngIf="show" formControlName="Id" [(ngModel)]="countryLookupId" class="width">
<ng-option [value]="country.id" *ngFor="let country of countries">{{ country.name_FL }} 
</ng-option>
</ng-select>
我假设有一个countryLookupId:string;你没有包括的财产

问题是this.countryLookupId应该是国家id,而不是国家名称。它应该匹配选项值,而不是显示文本

this.countryLookupId = this.company.country.id;
编辑:

而且您没有正确设置选项:

ng select希望您提供一系列项目:

items: [];

ngOnInit(): void {
  // some code to get countries
  //...


  this.items = countries.map.(x => ({ id: country.id, name: country.name_FL }));
}
编辑:

假设您现在直接使用country数组,我认为您的html应该是这样的:

<ng-select *ngIf="countries" [(ngModel)]="countryLookupId" class="width" 
 [items]="countries" bindValue="id" bindLabel="name_FL">
</ng-select>

我已经更改了它,但是它现在显示id并发送id,那么如何显示名称但发送id?我已经编辑了我的答案。阅读ng select文档时,您需要提供一个项目数组,而不是提供选项html.getCountries{this.restService.GetAllCountries.subscriberes:any=>{this.countries=res.data.lookups this.items=this.countries.mapx=>x;console.logthis.items}您没有遵循我的建议。项应该是{id,name}对象的数组。如果只想绑定到国家数组,还可以在ng select上设置bindValue和bindLabel。阅读文档。我道歉,我现在已将其更新为以下内容:this.items=this.countries.mapx=>{id:x.id,name:x.name\u FL};你能创造闪电战吗
<ng-select *ngIf="countries" [(ngModel)]="countryLookupId" class="width" 
 [items]="countries" bindValue="id" bindLabel="name_FL">
</ng-select>