Angular - привязка переменной к выбранному значению из выпадающего списка - PullRequest
0 голосов
/ 27 сентября 2018

Интересно, как связать переменную в зависимости от выбранного значения выпадающего меню.My dropdown list На данный момент у меня есть что:

this.moleCaracteristique.pourcentDavoirCaracteristique = this.mole.moleCaracteristiques[0].pourcentDavoirCaracteristique;

this.moleCaracteristique.pourcentDavoirCaracteristique всегда 70.

JSON

{
    "id": 1,
    "email": "test@gmail.com",
    "password": "passw",
    "nom": "titi",
    "moles": [
        {
            "id": 1,
            "commentaire": "bras droit",
            "nomFichier": "fichier",
            "moleCaracteristiques": [
                {
                    "id": 2,
                    "percentToHaveTheCaracteristic": 92,
                    "caracteristique": {
                        "id": 2,
                        "libelle": "milka"
                    }
                },
                {
                    "id": 1,
                    "percentToHaveTheCaracteristic": 70, //This is where the 70 comes from**
                    "caracteristique": {
                        "id": 1,
                        "libelle": "globule"
                    }
                }
            ]
        }
    ]
}

HTML

<div class="col-xs-12 marginBottom10" >
<b>Choisissez une caracteristique :</b> 
 <select class='select-option' required [(ngModel)]='optionSelected' (ngModelChange)='onOptionsSelected($event)'>
    <option class='option' *ngFor='let option of options' [value]="option">{{option.libelle}}</option>
</select>
</div>

<div class="text-center">

<button (click)="complete()"> Soumettre</button>

</div>
<div *ngIf = "optionSelected">
{{ this.moleCaracteristique.percentToHaveTheCaracteristic}}%
</div>

TS

 ngOnInit() {

    this.getCaracteristiques();

  }
  onOptionSelected(event) {
    console.log(event); // option value will be sent as event
  }
  openModal() {
    this.moleCaracteristique = new Object();
    this.moleCaracteristique.percentToHaveTheCaracteristic = this.mole.moleCaracteristiques[0].percentToHaveTheCaracteristic;
    }
  }

  getCaracteristiques(){
    this.caracteristiqueService.getAll().subscribe(caracteristiques => {
      this.options = caracteristiques;
    });
  }
}

Когда я выбираю "глобулу", она должна быть 70, но когда я выбираю "milka", он должен стать 92 ... Как мне этого добиться?

Спасибо очень много!

РЕДАКТИРОВАТЬ

Это JSON, из которого я получаю:

getCaracteristiques(){
    this.caracteristiqueService.getAll().subscribe(caracteristiques => {
      this.options = caracteristiques;
    });
  }

[
    {
        "id": 1,
        "libelle": "globule"
    },
    {
        "id": 2,
        "libelle": "milka"
    }
]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...