通过Angular NGXS的持续文本输入

发布于 2025-01-24 16:46:20 字数 1615 浏览 0 评论 0原文

我有一个可以远离的组件,但我希望文本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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2025-01-31 16:46:20

实施文本输入持久性...

输入控制:

<input type="text" class="form-control" id="country" required [ngModel]="(country$ | async)" name="country" (ngModelChange)="autoChange('country',$event)" />

状态类:

@State<FavouritesStateModel>({
  name: 'favouritesState',
  defaults: {
    country: ''
  }
})

@Injectable()
export class FavouritesState {
  @Action(Search.Country)
  Country(ctx: StateContext<FavouritesStateModel>, { country }: Search.Country) {
    ctx.setState(patch({
      country: country
    }))
  }

  @Selector()
  static getCountry(state: FavouritesStateModel) {
    return state.country
  }
}

和行动类:

export namespace Search {
  export class Country {
    static readonly type = '[Search] Set Country';
    constructor(public country: string) {}
  }
}

Implement text input persistence...

Input control:

<input type="text" class="form-control" id="country" required [ngModel]="(country$ | async)" name="country" (ngModelChange)="autoChange('country',$event)" />

State class:

@State<FavouritesStateModel>({
  name: 'favouritesState',
  defaults: {
    country: ''
  }
})

@Injectable()
export class FavouritesState {
  @Action(Search.Country)
  Country(ctx: StateContext<FavouritesStateModel>, { country }: Search.Country) {
    ctx.setState(patch({
      country: country
    }))
  }

  @Selector()
  static getCountry(state: FavouritesStateModel) {
    return state.country
  }
}

And the action class:

export namespace Search {
  export class Country {
    static readonly type = '[Search] Set Country';
    constructor(public country: string) {}
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文