如何在angular2中对活动表单执行CRUD操作和正确使用接口?

时间:2017-02-19 07:01:14

标签: arrays forms angular crud reactive-forms

我正在创建一个反应式表单数组来实现CRUD操作。经过长时间的谷歌搜索,我发现有几种方法可以实现这一点。 我的怀疑是:

  1. 如何使用货币界面制作数据模型 形式?
  2. 如何在按钮单击(addItem())上添加项目数组?
  3. 提交后,是否可以将模型导出为JSON?

    Github source code

  4. currency.interface.ts

    export interface Currency {
      name: string;
      date: string;
      address: string;
      remarks: string;
      serialNumber: number;
      idNumber: number;
      phoneNumber: number;
      mobileNumber: number;
      nationality: string;
      totalCost: number;
      tax: number;
      taxTotal: number;
      grandTotal: number;
      items: Item[];
    }
    
    export interface Item {
      currencyName: string;
      currencyType: string;
      currencyValue:string;
      total: number;
      presentRate:number;
    }
    

    currency.component.ts

    ngOnInit() {
        this.currencyType = ['CN & Coins']
        this.myForm = this.fb.group({
              name: ['', [Validators.required, Validators.minLength(2)]],
              date: ['', [Validators.required, Validators.minLength(2)]],
              address: ['', [Validators.required, Validators.minLength(2)]],
              remarks: ['', [Validators.required, Validators.minLength(2)]],
              serialNumber: ['', [Validators.required, Validators.minLength(1)]],
              idNumber: ['', [Validators.required, Validators.minLength(2)]],
              phoneNumber: ['', [Validators.required, Validators.minLength(2)]],
              mobileNumber: ['', [Validators.required, Validators.minLength(2)]],
              nationality: [''],
              tax: ['', [Validators.minLength(2)]],
              totalCost: [{ value: '', disabled: true }, [Validators.minLength(2)]],
              taxAmount: [{ value: '', disabled: true }, [Validators.minLength(2)]],
              grandTotal: [{ value: '', disabled: true }, [Validators.minLength(2)]],
              items: this.fb.array([
                  this.initItem(),
              ])
          });
        }
        initItem() {
          return this.fb.group({
              currency: [''],
              currencyName: [''],
              currencyType: [''],
              amount: [],
              presentRate:[''],
              total:[{ value: '', disabled: true }, [Validators.minLength(2)]]
          });
      }
    

    currency.component.html

    <div class="flex-item" fxFlex="50%">
            <md-card class="app-input-section" style="height: relative" >
              <md2-datepicker formControlName="date"></md2-datepicker>
              <div formArrayName="items">
                 <div let i=index>
                     <div [formGroupName]="i">
                       <md2-autocomplete [items]="currency"
                                 item-text="currency"
                                 (change)="handleChange($event, i)"
                                 placeholder="Currency purchased"
                                 formControlName="currency"
                                 >
                       </md2-autocomplete>
    
                       <md-select placeholder="Currency type" formControlName="currencyType">
                         <md-option *ngFor="let type of currencyType" [value]="type" >
                           {{type}}
                         </md-option>
                       </md-select>
                       <md-input-container >
                         <input mdInput placeholder="Amount" formControlName="amount">
                       </md-input-container>
                       <md-input-container >
                         <input mdInput placeholder="Present rate" [value]="presentRate" formControlName="presentRate"  >
                       </md-input-container>
                       <md-input-container >
                         <input mdInput placeholder="Rupee Equivalent" value={{getRupee()}} formControlName="total" >
                       </md-input-container>
                     </div>
                     <div class="flex-item" fxFlex >
                       <button md-raised-button color="primary" type="submit" >Save</button >
                       <button md-raised-button color="primary" (click)="addItem()">Add</button>
                       <button md-raised-button color="warn">Remove selected currency</button>
                       <button md-raised-button >Edit</button>
                     </div>
                 </div>
              </div>
            </md-card>
          </div>
    

0 个答案:

没有答案