通过Angular NGXS的持续文本输入
我有一个可以远离的组件,但我希望文本input
字段能够在这些导航更改之间维护其值。
输入
看起来像这样:
<input type="text" class="form-control" id="country" required [ngModel]="country" name="country" (ngModelChange)="autoChange('country',$event)" />
组件的相关部分是:
export class SearchComponent implements OnInit {
@Select(FavouritesState.getCountry) country$: Observable<string>;
country: string = '';
constructor(private store: Store) {
}
ngOnInit(): void {
this.country$.subscribe(c => {
this.country = c;
});
}
autoChange(field: string, $event: string): void {
switch (field) {
case 'country': {
this.country = $event;
this.store.dispatch(new Search.Country(this.country));
break;
}
}
}
我试图单向将传递到input
的值绑定到类变量country
和调度更改为this.store.dispatch()
更改键入文本。
州阶层看起来像这样:
@State<FavouritesStateModel>({
name: 'favouritesState',
defaults: {
country: ''
}
})
@Injectable()
export class FavouritesState {
@Action(Search.Country)
Country(ctx: StateContext<FavouritesStateModel>, action: string) {
ctx.setState(patch({
country: action
}))
}
@Selector()
static getCountry(state: FavouritesStateModel) {
return state.country
}
}
行动类看起来像这样:
export namespace Search {
export class Country {
static readonly type = '[Search] Set Country';
constructor(public country: string) {}
}
}
我对这种相当基本的(我认为)模式做了什么错?
I have a component which I can navigate away from but I want the text input
fields to maintain their values between these navigation changes.
The input
looks like this:
<input type="text" class="form-control" id="country" required [ngModel]="country" name="country" (ngModelChange)="autoChange('country',$event)" />
The relevant part of the component is this:
export class SearchComponent implements OnInit {
@Select(FavouritesState.getCountry) country$: Observable<string>;
country: string = '';
constructor(private store: Store) {
}
ngOnInit(): void {
this.country$.subscribe(c => {
this.country = c;
});
}
autoChange(field: string, $event: string): void {
switch (field) {
case 'country': {
this.country = $event;
this.store.dispatch(new Search.Country(this.country));
break;
}
}
}
I'm trying to one-way bind the value passed into the input
to class variable country
and dispatch changes to the typed text with this.store.dispatch()
.
The state class looks like this:
@State<FavouritesStateModel>({
name: 'favouritesState',
defaults: {
country: ''
}
})
@Injectable()
export class FavouritesState {
@Action(Search.Country)
Country(ctx: StateContext<FavouritesStateModel>, action: string) {
ctx.setState(patch({
country: action
}))
}
@Selector()
static getCountry(state: FavouritesStateModel) {
return state.country
}
}
And the action class looks like this:
export namespace Search {
export class Country {
static readonly type = '[Search] Set Country';
constructor(public country: string) {}
}
}
What am I doing wrong with this fairly basic (I thought) pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实施文本输入持久性...
输入控制:
状态类:
和行动类:
Implement text input persistence...
Input control:
State class:
And the action class: