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

角度-编辑时填充表单数组输入

  •  1
  • Abhijit  · 技术社区  · 8 年前

    我有一个数量字段和一个“添加”按钮。单击按钮时,将添加数量的新字段,并单击删除按钮将其删除。 我用了表单数组

    我的代码如下

    <div formArrayName="ingredients11">
     <!-- loop throught units -->
     <div *ngFor="let unit of recipeForm['controls'].ingredients11['controls']; let i = index ">
      <div [formGroupName]="i">
       <div class="form-group">
        <label>Quantity</label>
        <input type="text" class="form-control" formControlName="recipe_ingredient_quantity">
        <div *ngIf="unit['controls'].recipe_ingredient_quantity.invalid" class="alert alert-danger">
         <div *ngIf="unit['controls'].recipe_ingredient.errors.required">
          Quantity is required.
         </div>
        </div>
       </div>
       <button class="btn btn-danger" *ngIf="recipeForm['controls'].ingredients11['controls'].length > 1" (click)="remove(i)">Delete</button>
      </div>
     </div>
     <button class="btn btn-primary" (click)="add()">Add</button>
    </div>
    

    我还有一个编辑功能,在这里我想预先填充数量字段的数量和它们以前保存的编辑值

    我的代码如下:

    <div formArrayName="ingredients11">
      <!-- loop throught units -->
      <div *ngFor="let unit of editrecipeForm['controls'].ingredients11['controls']; let i = index ">
       <!-- row divider show for every nex row exclude if first row -->
       <div *ngIf="editrecipeForm['controls'].ingredients11['controls'].length > 1 && i > 0" ><hr></div>
       <div [formGroupName]="i" *ngFor="let ri of editingredientsarray;">
        <div class="form-group">
         <label>Ingredients</label>
         <select class="form-control" formControlName="recipe_ingredient" >
          <option value="">Select Ingredient</option>
          <option *ngFor="let ingredient of ingredients | async" [value]="ingredient.id">
           {{ingredient.name}}
          </option>
         </select>
         <div *ngIf="unit['controls'].recipe_ingredient.invalid" class="alert alert-danger">
          <div *ngIf="unit['controls'].recipe_ingredient.errors.required">
           Ingredient is required.
          </div>
         </div>
        </div>
        <div class="form-group">
         <label>Quantity</label>
         <input type="text" class="form-control" formControlName="recipe_ingredient_quantity" [value]="ri.quantity">
         <div *ngIf="unit['controls'].recipe_ingredient_quantity.invalid" class="alert alert-danger">
          <div *ngIf="unit['controls'].recipe_ingredient.errors.required">
           Quantity is required.
          </div>
         </div>
        </div>
        <button class="btn btn-danger" *ngIf="editrecipeForm['controls'].ingredients11['controls'].length > 1" (click)="removeEditIngredient(i)(i)">Delete Ingredient</button>
       </div>
      </div>
      <button class="btn btn-primary" (click)="addEditIngredient()">Add New Ingredient</button>
     </div>
    

    但该值不在输入字段中填充。有人能指点一下吗

    2 回复  |  直到 8 年前
        1
  •  1
  •   Vivek Doshi    8 年前

    您的状态配置错误:

    export const Users = [
      {
          "id": "1",
          "name": "aaa",
          "technology": "1" //<------ Use id instead of names
      },
      {
          "id": "2",
          "name": "bbb",
          "technology": "1,2" //<------ Use id instead of names
      },
      {
          "id": "3",
          "name": "ccc",
          "technology": "1,3" //<------ Use id instead of names
      }
    ]
    

    Working Demo

        2
  •  0
  •   Rohan Kumar    8 年前

    看来你把你的数量场和 ri.quantity 因此,必须在编辑函数中传递此对象,并更改同一对象中的值,即。 ri 在组件ts文件中,

    edit(ri){
        ri.quantity=SOME_NEW_VALUE_HERE;
    }
    

    已更新,用于在选项中添加选定属性,

    <option ... [selected]="ingredient.id==ri.ingredient">
       {{ingredient.name}}
    </option>
    
    推荐文章