从材料组合框中更新NGRX存储,然后绑定到返回的NGRX Store数据



我刚刚开始使用ngrx抓紧。我已经连接了各种控件以更新,然后从商店返回数据。文本框和复选框按预期工作,但是组合框是另一层。我无法正确地绑定。

这是我遇到的问题:

1)当我更新组合框中的值时,我的setCurrentBadge函数被触发了两次

2)我从组合框中选择的值选择的值 <mat-select class="badge-codes-combobox">不可见。

3)getCurrentBadge函数发射,即使它位于nginit Life-Cycle钩子中,并且页面尚未重新加载。

4)当我重新加载页面时,getCurrentBadge函数触发,但组合框未显示返回的值。

就代码而言,我需要badgecodelected($ event)即可在组合框的值更改时一次启动一次。我需要[value] =" selectedbadge"才能显示选定的值,并且在重新加载页面时,我需要组合框才能显示从商店返回的值)

这是我的代码。

    <div class="row">
  <div class="col-md-4">
    <app-declaration-type
    [errorMessage] = "errorMessage$ | async"
    [displayTypes] = "displayTypes$ | async"
    [declarationTypes] = "declarationTypes$ | async"
    [badges] = "badges$ | async"
    [traderReference]= "traderReference$ | async"
    [selectedBadge] = "selectedBadge$ | async"
    (badgeSelected) = "badgeCodeSelected($event)" 
    (checked) = "checkChanged($event)"
    (traderReferenceSet) = "onBlurTraderReferenceChange($event)"
    >
    </app-declaration-type>
  </div>
</div>

@Component({
  selector: 'app-declaration',
  templateUrl: './declaration-shell.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DeclarationShellComponent implements OnInit {
  errorMessage$: Observable<string>;
  displayTypes$: Observable<boolean>;
  declarationTypes$: Observable<Declarationtype[]>;
  badges$: Observable<Badge[]>;
  selectedDeclarationType$: Observable<string>;
  selectedBadge$: Observable<Badge>;
  traderReference$: Observable<string>;
  constructor(private store: Store<fromDeclaraionType.State>) {}
  ngOnInit() {
    this.store.dispatch(new fromDeclarationTypeActions.LoadDeclarationType());
    this.store.dispatch(new fromDeclarationTypeActions.LoadBadges());
    this.errorMessage$ = this.store.pipe(select(fromDeclaraionType.getError));
    this.displayTypes$ = this.store.pipe(
      select(fromDeclaraionType.getToggleDeclarationTypes)
    );
    this.declarationTypes$ = this.store.pipe(
      select(fromDeclaraionType.getDeclarationTypes)
    );
    this.selectedDeclarationType$ = this.store.pipe(
      select(fromDeclaraionType.getCurrentDeclarationType)
    );
    this.badges$ = this.store.pipe(select(fromDeclaraionType.getBadges));
    this.selectedBadge$ = this.store.pipe(
      select(fromDeclaraionType.getCurrentBadge),
      tap(x => console.log('About to fetch current badge from store {0}', x))
    );
    this.traderReference$ = this.store.pipe(
      select(fromDeclaraionType.getTraderReference)
    );
  }
  checkChanged(value: boolean): void {
    console.log('About to dispatch toggle Display Declaration Types');
    this.store.dispatch(
      new fromDeclarationTypeActions.ToggleDeclarationTypes(value)
    );
  }
  onBlurTraderReferenceChange(value: string) {
    console.log('About to dispatch Set Trader Reference');
    this.store.dispatch(
      new fromDeclarationTypeActions.SetTraderReference(value)
    );
  }
  badgeCodeSelected(value: Badge) {
    console.log('About to dispatch Set Current Badge');
    console.log(value);
    this.store.dispatch(new fromDeclarationTypeActions.SetCurrentBadge(value));
  }
}

 <div class="declaration-type">
  <mat-accordion>
    <mat-expansion-panel
      [expanded]="displayTypes === true"
      (opened)="(displayTypes === true)"
    >
      <mat-expansion-panel-header
        [collapsedHeight]="customCollapsedHeight"
        [expandedHeight]="customExpandedHeight"
      >
        <mat-panel-title> <h4>Declaration Type</h4> </mat-panel-title>
        <label>
          <input
            class="form-check-input"
            type="checkbox"
            (change)="checkChanged($event.target.checked)"
            [checked]="displayTypes"
          />
          Display Types?
        </label>
      </mat-expansion-panel-header>
      <div class="controls-wrapper">
        <div class="flex-container">
          <div class="flex-item-declarationType">
            <label class="field-label labelAlignment">
              Decln Type [01]:
              <mat-select class="declaration-type-combobox" [value]="selectedDeclarationType">
                <mat-option
                  *ngFor="let declarationType of declarationTypes"
                  [value]="declarationType?.value"
                >
                  {{ declarationType.value }}
                </mat-option>
              </mat-select>
            </label>
          </div>
          <div class="flex-item-badgeCode">
            <label class="field-label labelAlignment">
              Badge Codes:
              <mat-select class="badge-codes-combobox" [value]="selectedBadge" >
                <mat-option (onSelectionChange)="badgeCodeSelected(badge)"
                  *ngFor="let badge of badges"
                  [value]="badge?.code">
                 <div>{{ badge.code }} - {{ badge.name }}</div>
                </mat-option>
              </mat-select>
            </label>
          </div>
        </div>
        <div class="flex-container">
          <div class="flex-item-traderReference">
            <label class="field-label labelAlignment">
              Trader Reference [07]:
              <input
                matInput
                type="text"
                [(ngModel)]="traderReference"
                class="trader-reference-inputBox"
                (blur)="onTraderReferenceSet(traderReference)"
              />
              <button
                mat-button
                *ngIf="traderReference"
                matSuffix
                mat-icon-button
                aria-label="Clear"
                (click)="traderReferenceValue = ''"
              >
                <mat-icon>close</mat-icon>
              </button>
            </label>
          </div>
        </div>
      </div>
    </mat-expansion-panel>
  </mat-accordion>
</div>

@Component({
  selector: 'app-declaration-type',
  templateUrl: './declaration-type.component.html',
  styleUrls: ['./declaration-type.component.scss']
})
export class DeclarationTypeComponent implements OnInit {
  customCollapsedHeight = '40px';
  customExpandedHeight = '40px';
  @Input() errorMessage: string;
  @Input() displayTypes: boolean;
  @Input() declarationTypes: Declarationtype[];
  @Input() badges: Badge[];
  @Input() selectedDeclarationType: string;
  @Input() selectedBadge: Badge;
  @Input() traderReference: string;
  @Output() checked = new EventEmitter<boolean>();
  @Output() declarationTypeSelected = new EventEmitter<string>();
  @Output() badgeSelected = new EventEmitter<Badge>();
  @Output() traderReferenceSet = new EventEmitter<string>();
  constructor() {}
  ngOnInit(): void {}
  checkChanged(value: boolean): void {
    this.checked.emit(value);
  }
  badgeCodeSelected(value: Badge) {
    this.badgeSelected.emit(value);
  }
  onTraderReferenceSet(value: string) {
    this.traderReferenceSet.emit(value);
  }
}

这对您来说会有所转变,但是我想分享我有一个库,除其他外,还具有内置指令将形式控件绑定到商店。最大的转变是您需要构建StoreObject而不是使用选择器,并且您不会写或处理任何操作。

库称为 ng-app-state,绑定到形式控件的键在nasModel指令中。我在下面给你一个想法。

假设您的复选框的值在商店中的一个地方:

class DeclarationTypeState {
  badgeCode: string;
}

然后您会这样使用:

@Component({
  template: `
    <mat-select [nasModel]="badgeCodeStore">
      <mat-option ...></mat-option>
    </mat-select>
  `,
})
export class DeclarationTypeComponent {
  badgeCodeStore: StoreObject<string>;
  constructor(myStore: MyStore) {
    this.badgeCodeStore = myStore('keyForDeclarationTypeState')('badgeCode');
  }
}

这将对您的商店进行2条绑定,以使mat-select与商店保持同步,并在用户更改商店中的商店中编辑值。

最新更新