首页 > 解决方案 > 使用角度模糊时的新数组

问题描述

我正在使用选择。他看到 select 中选择的内容做了一个 get 并检查以根据 select 带来值。问题是在模糊底部类别时使用 this.categoryList = [] 会清除顶部类别,如果我不使用它,它将在数组中增加。我希望在单击选择时根据获取显示选项。

COMPONENT TS  

//categoryInit() in ngOnit//
categoryInit() {
    this.initcategory((<HTMLInputElement>document.getElementById("category")).value)
  }
  
  getcategory(index?:any) {
    const value = (<FormArray>this.feedstockForm.value.feedstock[index].category);
    console.log(this.categoryList)
    this.initcategory(value) 
  }
  initcategory(value) {
    console.log(value)
    const totalResults = 0;
    const searchModel = {
      text: '',
      status: '',
    };
    this.feedstockService.getFeedstock(totalResults, searchModel)
    .subscribe(response => {
      const feedstocks = response.results.filter(x => x.category === value)
      feedstocks.forEach(feedstock => this.categoryList.push(feedstock.name))
     })
  }
<ng-container  [formGroup]="feedstockForm">
          <ng-container formArrayName="feedstock">
            <accordion [closeOthers]="oneAtATime" *ngFor="let fcFeedStock of feedstockForm.get('feedstock')?.controls; let i = index">
              <accordion-group  heading="{{ fcFeedStock.get('position').value }} / {{ fcFeedStock.get('category').value }}" [formGroupName]="i">
                <div>
                  <i class="material-icons close-category" (click)="removeFeedstockCategory(i)">
                    close
                  </i>
                </div>
                <div class="divide-section">
                  <div class="first-column">
                    <div class="header-section options">
                      <span>ADICIONAR COMPONENTE</span>
                      <i class="material-icons" (click)="addFeedstockComponent(i)">
                        add
                      </i>
                    </div>
                      <div class="form-group">
                        <label for="category">CATEGORIA</label>
                        <select class="form-control" id="category" (change)=getcategory(i) formControlName="category">
                          <option value="" selected ></option>
                          <option *ngFor="let cat of categories" [value]="cat.value">
                            {{cat.viewValue}}
                          </option>
                        </select>

标签: javascriptangulartypescript

解决方案


推荐阅读