代码之家  ›  专栏  ›  技术社区  ›  Abhijit

角度-在下拉列表中设置所选值

  •  0
  • Abhijit  · 技术社区  · 8 年前
    <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>
    

    ri.ingredient=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 ]
        });
      } 
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Vivek Doshi    8 年前

    ngModel select

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

    WORKING DEMO

    推荐文章