2017-08-07 11 views
0

ロダッシュチェーンと約束を使ってチェーンの文字列操作を成功させようとしています。私のrefColors.lengthは常に0です。文がテストされた後にサービスが呼び出されているので、if文は決して入力しません。私は新しい約束で、その後(のすべてのチェーン部分を入れようとしていロダッシュチェーンを使用してデータを同期させる約束

initiateModalData(refColorList: any) { 

    refColorList.forEach((values, index) => { 
     const listRefRows: Array<GblRowValues> = []; 
     const initialcolors: Array<any> = []; 
     const refColors: Array<any> = []; 
     const headList: Array<any> = []; 
     this.rowId = 1; 

     _(value) 
       .chain() 
       .tap((colors) => { 
        this.myAPIService.getStyle(colors[index].id) 
         .subscribe(styleRef => { 
          if (styleRef) { 
           styleRef.colors.map(color => { 
            refColors.push(color.colorId); 
           }); 
          } 
         }); 
       }) 
       .forEach(refSize => { 
        refSize.headList.forEach(item => { 
         headList.push({ 
          size: item.size, 
          value: 0 
         }); 
        }); 
       }) 
       .forEach(val => { 
        initialcolors.push(val.color); 
       }) 
       .forEach((row) => { 
        this.rowId += 1; 
        listRefRows.push(new rowValue(row.headList, initialcolors, this.rowId)); 
       }) 
       .value(); 

      const factory = this.componentFactoryResolver.resolveComponentFactory(ReferenceTableComponent); 
      const ref = this.target.createComponent(factory); 
      console.log(refColors.length) 
      ref.instance.listRows = listRefRows; 
      ref.instance.rowId = this.rowId; 
      ref.instance.headList = _.uniqBy(headList, 'size'); 
      if (_.difference(refColors, initialcolors).length !== 0) { 
       ref.instance.colors = _.difference(refColors, initialcolors); 
       ref.instance.isAddRowValid = true; 
      } 
    }); 
    } 

私の機能で行おうと何

)私は私のcomponantを開始しますが、私のcomponantはデータなしで開始され

return new Promise((resolve, reject) => { 
     _(value) 
       .chain() 
       .tap((colors) => { 
        this.myAPIService.getStyle(colors[index].id) 
         .subscribe(styleRef => { 
          if (styleRef) { 
           styleRef.colors.map(color => { 
            refColors.push(color.colorId); 
           }); 
          } 
         }); 
       }) 
       .forEach(refSize => { 
        refSize.headList.forEach(item => { 
         headList.push({ 
          size: item.size, 
          value: 0 
         }); 
        }); 
       }) 
       .forEach(val => { 
        initialcolors.push(val.color); 
       }) 
       .forEach((row) => { 
        this.rowId += 1; 
        listRefRows.push(new rowValue(row.headList, initialcolors, this.rowId)); 
       }) 
       .value(); 

}) 
    .then (() => { 
    const factory = this.componentFactoryResolver.resolveComponentFactory(ReferenceTableComponent); 
      const ref = this.target.createComponent(factory); 
      console.log(refColors.length) 
      ref.instance.listRows = listRefRows; 
      ref.instance.rowId = this.rowId; 
      ref.instance.headList = _.uniqBy(headList, 'size'); 
      if (_.difference(refColors, initialcolors).length !== 0) { 
       ref.instance.colors = _.difference(refColors, initialcolors); 
       ref.instance.isAddRowValid = true; 
      } 
    }) 

答えて

0

私が加入した後、私のcomponantを開始し、最後のforEach後のタップを()を使用して、私の問題を解決するには、そのように実行されます。

   .tap((colors) => { 
       this.myAPIService.getStyle(colors[index].id) 
        .subscribe(styleRef => { 
          if (styleRef) { 
           styleRef.colors.map(color => { 
            refColors.push(color.colorId); 
           }); 
          } 
         }, 
          error => this.myNotificationService.error(`${error} !`, 'error'), 
         () => { 
          const factory = this.componentFactoryResolver.resolveComponentFactory(ReferenceTableComponent); 
          const ref = this.target.createComponent(factory); 
          ref.instance.listRows = listRefRows; 
          ref.instance.rowId = this.rowId; 
          ref.instance.headList = _.uniqBy(headList, 'size'); 
          if (_.difference(refColors, initialcolors).length !== 0) { 
           ref.instance.colors = _.difference(refColors, initialcolors); 
          } 
         } 
        ); 
      }) 
関連する問題