我才刚开始接触Ngrx。我已经连接了各种控件以进行更新,然后从我的商店中返回数据。文本框和复选框可以按预期工作,但是组合框是另一层。我无法正确绑定。
这是我面临的问题:
1)当我更新组合框中的值时,setCurrentBadge函数被触发两次
2)我从组合框中的列表中选择的值称为
<Dialog
PaperProps={{ component: PaperComponent, dialogRef: this.dialogRef }}
>
不可见。
3)触发getCurrentBadge函数,即使该函数位于ngInit生命周期挂钩中并且页面尚未重新加载。
4)重新加载页面时,会触发getCurrentBadge函数,但组合框不会显示返回的值。
就代码而言,当组合框的值更改时,我需要badgeCodeSelected($ event)触发一次。我需要[value] =“ selectedBadge”来显示选定的值,并且在重新加载页面时,我需要组合框来显示从商店返回的值)
这是我的代码。
<mat-select class="badge-codes-combobox">
<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>
答案 0 :(得分:0)
这对您来说将是一个转变,但是我想分享一下我拥有一个库,该库除其他外还具有将表单控件绑定到商店的内置指令。最大的变化是,您需要构建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');
}
}
这将对您的商店进行两向绑定,以使mat-select
与商店保持同步,并在用户更改商店名称时对其进行编辑。