Angular - установить выбранное значение в раскрывающемся списке

<select class="form-control" formControlName="recipe_ingredient">
          <option value="">Select Ingredient</option>
          <option *ngFor="let ingredient of ingredients | async" [value]="ingredient.id" [selected]="ingredient.id == ri.ingredient">
           {{ingredient.id}}-{{ingredient.name}}
          </option>

р.ингредиент = 2; Но ниже не возвращает true в приведенном выше коде, поэтому значение не выбрано

ingredient.id == ri.ingredient 

может ли кто-нибудь направить

Код в моем файле ts выглядит следующим образом:

editRecipeForm() {
    this.editrecipeForm = this.fb.group({
      recipe_name: ['', Validators.required ],
      recipe_description: ['', Validators.required ],
      edit_recipe_image: [],
      ingredients11: this.fb.array([
         this.getIngredient()
      ])
   });
  } 12:49 
getIngredient() {
    return this.fb.group({
     recipe_ingredient: ['', Validators.required ],
      recipe_ingredient_quantity: ['', Validators.required ]
    });
  } 

person Abhijit    schedule 01.08.2018    source источник
comment
Вы пытались установить <option ng-selected="ingredient"></option> этот элемент в коллекции   -  person Eldho    schedule 01.08.2018
comment
@Элдхо это не работает   -  person Abhijit    schedule 02.08.2018


Ответы (1)


Я думаю, все, что вам нужно, это ngModel в теге select:

<select class="form-control" formControlName="recipe_ingredient" [(ngModel)]="ri.ingredient">

РАБОЧАЯ ДЕМО

person Vivek Doshi    schedule 02.08.2018
comment
Я попробовал, но он возвращает ошибку - main.9c580da952e68217c3e4.js:1 ERROR TypeError: Не удается прочитать свойство 'required' null в Object.updateDirectives (main.9c580da952e68217c3e4.js:1) в Object.updateDirectives Я разместил подробный код на stackoverflow.com/questions/51625636 / Также обновлен вопрос с кодом файла ts. Не могли бы вы проверить, не пропустил ли я что-нибудь - person Abhijit; 02.08.2018
comment
Я создал демо на stackblitz.com/edit/ Пожалуйста, проверьте. и дайте мне знать решение - person Abhijit; 02.08.2018