проблема с сохранением значения в качестве идентификатора в раскрывающемся списке angular - PullRequest
0 голосов
/ 29 апреля 2020

У меня выпадающий список, который используется для заполнения списка стран и штатов при выборе списка стран конкретной страны. Работает отлично. Моя проблема заключается в том, что я хочу сохранить идентификатор страны при отправке. Но теперь он сохраняется как объект с полным идентификатором списка, именем, датой создания, временем и т. Д. c .. Мой код указан ниже

<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 = [];
    }

Результат

страна: созданАт: " 2020-04-02T14: 28: 28.924Z "id: 2 name:" Albania "updatedAt:" 2020-04-02T14: 28: 28.924Z "

ожидаемый результат:

страна: 2

1 Ответ

0 голосов
/ 30 апреля 2020

Я вижу проблему в вашем коде. Вам не нужно передавать countryList и страну в методе changeState. Пожалуйста, замените это в вашем коде:

В HTML Замените это

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

на

(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 = [];
    }

с

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 = [];
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...