在下拉角中将值保存为id的问题

问题描述 投票:0回答:1

我有一个下拉列表,用于在选择特定国家/地区状态列表时填充国家和州列表。运行良好。我的问题是提交时我想保存国家/地区的ID。但是现在它被保存为具有完整列表ID,名称,创建日期,时间等的对象。我的代码如下

<ng-autocomplete [data]="countryList" formControlName="country"
                             (selected)="changeState($event,countryList,'country')"

                            [searchKeyword]="keywordCountry" placeHolder="Enter country name"
                             [itemTemplate]="countryTemplate"
                             [notFoundTemplate]="notFoundTemplate">
            </ng-autocomplete>

            <ng-template #countryTemplate let-item value="id" >
              <a  [innerHTML]="item.name" ></a>
            </ng-template>


            <ng-template #notFoundTemplate let-notFound>
              <div [innerHTML]="notFound"></div>
            </ng-template>


changeState(event,countryId) {
console.log('event');
console.log('countryId');
if (countryId && event) {
  let _this = this;
 _this.stateList = [];
  var getId = typeof event == 'object' ? event.id : event;
  var countryId = _.find(countryId, {id: parseInt(getId, 10)});
  console.log("countryId", countryId);
  this.selectedCountry = countryId.id;
  console.log('this.selectedCountry',this.selectedCountry);


  _this.CS.getStateList({ id: countryId.id }).subscribe(response => {
    console.log("response",response);
    if (response && response.status == "success" && response.result && response.result.length > 0) {
      _this.stateList = response.result[0].CS;
      console.log("_this.stateList",_this.stateList);
    } else {
    //  _this.stateList = [];
    }

结果

国家:createdAt:“ 2020-04-02T14:28:28.924Z”id:2名称:“阿尔巴尼亚”更新时间:“ 2020-04-02T14:28:28.924Z”

预期结果:

国家:2

angular angular7
1个回答
0
投票

我可以在您的代码中看到此问题。您无需在changeState方法中传递countryList和country。请替换为您的代码:

在HTML中替换为

(selected)="changeState($event,countryList,'country')"

with

(selected)="changeState($event)"

以及您的ts文件中

changeState(event,countryId) {
console.log('event');
console.log('countryId');
if (countryId && event) {
  let _this = this;
 _this.stateList = [];
  var getId = typeof event == 'object' ? event.id : event;
  var countryId = _.find(countryId, {id: parseInt(getId, 10)});
  console.log("countryId", countryId);
  this.selectedCountry = countryId.id;
  console.log('this.selectedCountry',this.selectedCountry);


  _this.CS.getStateList({ id: countryId.id }).subscribe(response => {
    console.log("response",response);
    if (response && response.status == "success" && response.result && response.result.length > 0) {
      _this.stateList = response.result[0].CS;
      console.log("_this.stateList",_this.stateList);
    } else {
    //  _this.stateList = [];
    }

with

changeState(event) {
   const countryId = event.id;
  _this.CS.getStateList({ id: countryId }).subscribe(response => {
    console.log("response",response);
    if (response && response.status == "success" && response.result && response.result.length > 0) {
      _this.stateList = response.result[0].CS;
      console.log("_this.stateList",_this.stateList);
    } else {
    //  _this.stateList = [];
    }
© www.soinside.com 2019 - 2024. All rights reserved.