微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

同一输入 Angular11 中的 FormControl 和 FormControlName

如何解决同一输入 Angular11 中的 FormControl 和 FormControlName

我处理了我的代码,因为我正在尝试使用 formControl 进行自动完成,但是当您选择某些内容到我的表单组数组时,我需要将这些数据带入。我想知道是否存在一种方法,我可以将 formControl 用于过滤我的自动完成和 formControlName 用于获取数据,谢谢,这是我的代码

        newArticle: FormControl = this.fb.control('',Validators.required);
      registerForm: FormGroup = this.fb.group({
        itemRows: this.fb.array([
          this.fb.group({
            article: [,Validators.required],quantity: [,price: [,discount: [,subtotal: [0,warehouse: ['',})
        ]),tax: [,date: [this.datetoday,Validators.required]
      });
  get rowsArr() {
    return this.registerForm.get('itemRows') as FormArray;
  }

ngOnInit(): void {
    this.filteredOptionsArticle = this.newArticle.valueChanges
      .pipe(
        startWith(''),map(value => typeof value === 'string' ? value : value.descripcion),map(descripcion => descripcion ? this._filtera(descripcion) : this.ArticleList.slice())
      );
    this.article.listArticles().subscribe((result) => {
      this.ArticleList = result;
    })
}

  addNewRow() {
    //if( this.registerForm.invalid ) { return; }
    const newRow = this.fb.group({
      article: [,warehouse: ["",Validators.required]
    })
    this.rowsArr.push(newRow);
  }
  deleteRow(rowIndex: number) {
    const control = <FormArray>this.registerForm.controls['itemRows'];
    if (control != null) {
      this.totalRow = control.value.length;
    }
    if (this.totalRow > 1) {
      control.removeAt(rowIndex);
    } else {
      alert('you need to write all')
    }
  }

private _filtera(name: string): IArticles[] { const filterValue = name.toLowerCase(); 返回 this.ArticleList.filter(option => option.descripcion.toLowerCase().indexOf(filterValue) === 0); }

还有我的 HTML:

<h2 mat-dialog-tittle>New Quotation</h2>
<mat-dialog-content>
    <form [formGroup]="registerForm">
        <div class="row" style="justify-content: center;">
            <div class="col-lg-3">
                <mat-form-field>
                    <mat-label>Type customer</mat-label>
                    <ng-container formArrayName="itemRows">
                        <input type="text" matInput [matAutocomplete]="autoCustomer" [formControl]="newCustomer">
                    </ng-container>
                    <mat-autocomplete #autoCustomer="matAutocomplete" [displayWith]="displayFnCustomer">
                        <mat-option *ngFor="let customer of filteredOptionsCustomer | async" [value]="customer">
                            {{customer.doc_nit}} - {{customer.nombre}}
                        </mat-option>
                    </mat-autocomplete>
                </mat-form-field>
            </div>
            <div class="col-lg-3">
                <mat-form-field>
                    <mat-label>Tax (%)</mat-label>
                    <input type="number" placeholder="0" matInput formControlName="tax" (keyup)="calculation()">
                </mat-form-field>
            </div>
            <div class="col-lg-3">
                <mat-form-field appearance="fill">
                    <mat-label>Choose a date</mat-label>
                    <input matInput [matDatepicker]="picker" formControlName="date">
                    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                    <mat-datepicker #picker></mat-datepicker>
                </mat-form-field>
            </div>
        </div>
        <table>
            <tr>
                <th>
                    <mat-label>Warehouse</mat-label>
                </th>
                <th>
                    <mat-label>Articles</mat-label>
                </th>
                <th>
                    <mat-label>Quantities</mat-label>
                </th>
                <th>
                    <mat-label>Prices</mat-label>
                </th>
                <th>
                    <mat-label>discounts</mat-label>
                </th>
                <th>
                    <mat-label>Subtotals</mat-label>
                </th>
                <th colspan="2">
                    <mat-label>Options</mat-label>
                </th>
            </tr>
            <ng-container formArrayName="itemRows" class="row">
                <ng-container *ngFor="let itemrow of rowsArr.controls; let i=index" [formGroupName]="i">
                    <tr>
                        <td style="width: 160px;">
                            <select class="form-control" formControlName="warehouse">
                                <option selected value="">
                                    Select warehouse
                                </option>
                                <option *ngFor="let warehouse of WarehouseList" [value]="warehouse.id_almacen">
                                    {{warehouse.nombre}}
                                </option>
                            </select>
                        </td>
                        <td style="text-align: left; width: 300px;">
                            <input type="text" class="form-control" name="" placeholder="Type Article"
                                [matAutocomplete]="autoArticle" [formControl]="newArticle">   <----- Here I woud like to do it
                            <mat-autocomplete #autoArticle="matAutocomplete" [displayWith]="displayFnArticle">
                                <mat-option *ngFor="let articles of filteredOptionsArticle | async" [value]="articles">
                                    {{articles.codigo}} - {{articles.descripcion}}
                                </mat-option>
                            </mat-autocomplete>
                        </td>
                        <td>
                            <input class="form-control" placeholder="0" type="text" (keyup)="calculation()"
                                formControlName="price">
                        </td>
                        <td>
                            <input class="form-control" placeholder="0" type="text" (keyup)="calculation()"
                                formControlName="discount" (keyup.enter)="addNewRow()">
                        </td>
                        <td>
                            <input class="form-control" placeholder="0" type="text" (keyup)="calculation()"
                                formControlName="quantity">
                        </td>
                        <td style="width:100px">
                            <div style="margin: 5px; text-align: center;">
                                {{itemrow.value.subtotal}}
                            </div>
                        </td>
                        <td>
                            <a (click)="deleteRow(i)">
                                <mat-icon>cancel</mat-icon>
                            </a>
                        </td>
                        <td>
                            <button mat-raised-button (click)="addNewRow">
                                +
                            </button><a (click)="addNewRow()">
                                <mat-icon>add_circle</mat-icon>
                            </a>
                        </td>
                    </tr>
                    <input type="text" formControlName="article" value="newArticle.value">

                </ng-container>
            </ng-container>
        </table><br>
        <button mat-raised-button (click)="register()">Register</button>
        <mat-card-actions>
            <button mat-raised-button mat-dialog-close>Close</button>
        </mat-card-actions>
    </form>
    <pre>
        {{registerForm.valid}}

        {{registerForm.value | json}}
        {{newArticle.value | json}}
        {{newArticle.value.id_articulo}}
        {{newCustomer.value | json}

}

</pre>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。