【问题标题】:Angular Observable chainingAngular Observable 链接
【发布时间】:2020-03-01 09:33:26
【问题描述】:

我所做的是:

A   B   C   D   E   F
----------------------
2   4   6   2   5   7
3   3   6   0   6   8
3   6   5   1   6   8
5   0   1   1   1   5
1   2   2   0   1   5


WHY I AM Subscribing B to A , C-> B , because on change of A , B will change and on change of B -> C will change (its like excel) where there are formulas on each field and change of each field other field will change 
ObservableB[i] = ObervableA[i];

ObservableC[i] = ObservableB[i].pipe();

ObservableD[i] = obbservableC[i].pipe(
Map(res => res +1)
);

ObservableE[i] = ObservableD[i].pipe();
ObservableF[i] = ObservableE[i].pipe();

ObserveB = dropdown Observable
ObserveC = dropdown Observable
ObserveD = Dropdown Observable
ObserveE = Drop down Observable
ObserveF = dropdown Observable

Now:

ObserveA = ObservableA.pipe(
  map(val => 
  {
    //map only unique values of ObservableA to ObserveA
   }
  )
);

同样适用于 ObserveB、ObserveC、ObserveD、ObserveE、ObserveF

现在我想要的是: 在选择 ObserveA 或任何其他下拉列表时,其他下拉列表值应更改 例如,如果我选择了

注意:下拉菜单是多选

DropdownA values selected : 2,3

所以其他下拉列表值应该自动重置(通过重置我的意思是可用选项应该更改为其他下拉列表)到

means for B dropdown available options should be : 4,3,6

ObserveC = 6,5 (only unique values in drop down)
ObserveD = 2,0,1
ObserveE = 5,6  (only unique values in drop down)
ObserveF = 7,8  (only unique values in drop down)```



Is this is achievable ??

【问题讨论】:

    标签: angular asynchronous angular2-observables unsubscribe


    【解决方案1】:

    下面的代码怎么样?

    exclude.pipe.ts:

      @Pipe({ name: 'exclude' })
      export class ExcludePipe implements PipeTransform {
    
        transform(value: any[], ...args: any[]): any {
          return value.filter(x => ![].concat.apply([], args).includes(x))
        }
    
      }
    

    unique.pipe.ts:

     @Pipe({ name: 'unique' })
     export class UinquePipe implements PipeTransform {
    
       onlyUnique = (value, index, self) => self.indexOf(value) === index;
    
       transform(value: any[]: any {
         return value.filter(this.onlyUnique)
       }
    
     }
    

    app.component.ts:

    @Component(...)
    export class AppComponent {
    
      observableA = of([2, 4, 6, 2, 5, 7])
      observableB = of([3, 3, 6, 0, 6, 8])
      observableC = of([3, 6, 5, 1, 6, 8])
      observableD = of([5, 0, 1, 1, 1, 5])
      observableE = of([1, 2, 2, 0, 1, 5])
    }
    

    app.component.html:

    <ng-container *ngIf="observableA | async as A">
      <ng-container *ngFor="let item of A | unique"> <td> {{ item }} </td></ng-container><br>
      <ng-container *ngFor="let item of (observableB | async | unique | exclude :A)"> <td> {{ item }} </td></ng-container><br>
    
      <ng-container *ngIf="observableB | async as B">
        <ng-container *ngFor="let item of (observableC | async | unique | exclude :A:B)"> <td> {{ item }} </td></ng-container><br>
    
        <ng-container *ngIf="observableC | async as C">
          <ng-container *ngFor="let item of (observableD | async | unique | exclude :A:B:C)"><td> {{ item }} </td></ng-container><br>
    
          <ng-container *ngIf="observableD | async as D">
            <ng-container *ngFor="let item of (observableE | async | unique | exclude : A:B:C:D)"><td> {{ item }} </td></ng-container><br>
    
          </ng-container>
        </ng-container>
      </ng-container>
    </ng-container>    
    

    【讨论】:

    • 认真感谢您的努力但是我想提醒您注意两件事:1)我这样做的原因是因为:“ObservableB[i] = ObervableA[i];”当 A 的值发生变化时,B 会根据类似于 excel 的一些公式进行评估 2)其次不仅要更改 otherdropdowns 的 A 下拉值应该更改,而且它的通用性,任何下拉更改都应该导致其他下拉列表的值更改 3)您已经根据每一行添加了一个 Observable ???
    • 如果您不明白请告诉我
    • 在这里我必须创建类似 excel 的结构,其中我有公式一,每列的值将根据同一行其他列计算,我可以实现其他事情,但留下下拉内容跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多