简体   繁体   English

如何使用 state 管理订阅 Angular 中的异步数组?

[英]How to subscribe to an async array in Angular with state management?

I have a dynamical list of items in my Angular app, a table of rows which look approximately like this:我在我的 Angular 应用程序中有一个动态的项目列表,一个看起来大约像这样的行表:

 Product name [edit button]   Date changed  [delete]
 Product name [edit button]   Date changed  [delete]
 ....

I am using Elf to maintain the list (could use rxAngular for that matter, but it does not have built in support for lists of items, especially so lists of UI items. Didn't want to go for anything more sophisticated as NGXS/NGRX).我正在使用 Elf 来维护列表(可以使用 rxAngular 来解决这个问题,但它没有内置对项目列表的支持,尤其是 UI 项目列表。不想 go 像 NGXS/NGRX 这样更复杂的东西)。 I do use however, rxAngular for side effects only, this does not really matter for my question.但是,我确实使用 rxAngular 仅用于副作用,这对我的问题并不重要。 When you click the [edit button] — you enter the "edit mode" — the Product name turns into an input box.当你点击[edit button] ——你进入“编辑模式”—— Product name变成一个输入框。 After editing, Date changed changes appropriately.编辑后, Date changed更改相应更改。

(1) You don't see the problem with State Management until you attempt to add animation. (1) 在您尝试添加 animation 之前,您看不到 State 管理的问题。 The list of items is an observable, obtained from the Elf store (could have been any other store).项目列表是可观察的,从 Elf 商店(可能是任何其他商店)获得。 As soon as you attempt to change anything, say enter the "edit mode", the store, according to the celebrated immutability principle, creates a new state.一旦您尝试更改任何内容,比如进入“编辑模式”,商店就会根据著名的不变性原则创建一个新的 state。 This means the list observable fires a new value, and since the list has changed, Angular has to redraw everything .这意味着列表 observable 会触发一个新值,并且由于列表已更改,因此 Angular 必须重新绘制所有内容 With animation on, you see the rows dancing before they rearrange into the resulting state (all the rows are deleted, then new copies added).启用 animation 后,您会看到行在重新排列到生成的 state 之前跳舞(删除所有行,然后添加新副本)。 If only one property of the row is changed (say the Date changed ), then that property should re–render (which does not mean deletion and creation, just the change of content).如果仅更改了行的一个属性(例如Date changed ),则该属性应重新呈现(这并不意味着删除和创建,只是内容的更改)。 How do you achieve that?你如何做到这一点?

(2) It seems more and more to me that these State Management libraries are much more necessary in React than in Angular. (2) 在我看来,这些 State 管理库在 React 中比在 Angular 中更需要。 Angular has change detection. Angular 有变化检测。 So if you type something in the input box, the model will already have the value.因此,如果您在输入框中键入内容,model 将已经具有该值。 Similarly, if you delete one row from a list, Angular will know that the other rows haven't changed.同样,如果您从列表中删除一行,Angular 将知道其他行没有更改。 In this way, it does not need the store (for that matter, even the observables are not necessary here, but of course the list of items is async, so it will be an observable).这样,它就不需要 store(就此而言,这里甚至不需要 observables,但当然项目列表是异步的,因此它将是一个 observable)。 One solution to using a store and not having the entire page re–render on any bit of change, is to use distinctUntilChanged() on every property that is being used in each row (product name, date etc ).使用商店而不让整个页面在任何更改时重新呈现的一种解决方案是在每行中使用的每个属性(产品名称、日期)上使用distinctUntilChanged() )。 This is done automatically if you use select() from the store.如果您使用商店中的select() ,这将自动完成。 However, I don't know if that's possible to do when you have a list of items…但是,我不知道当你有一个项目列表时是否可以这样做......

Easiest way to subscribe to your array is simply by using an async pipe.订阅阵列的最简单方法是使用异步 pipe。

In your HTML all you will have to do is {{yourObservableArray$ | async}}在您的 HTML 中,您所要做的就是{{yourObservableArray$ | async}} {{yourObservableArray$ | async}} . {{yourObservableArray$ | async}} The async pipe will handle listening to any changes, as well as disposing of itself.异步 pipe 将处理监听任何更改,以及自行处理。

One thing to note, and you mentioned that when you click edit you make a new state, is that is incorrect.需要注意的一件事,您提到当您单击编辑时,您会创建一个新的 state,这是不正确的。 You return an updated state from your reducer ONLY if any of the value that you are tracking has been changed.仅当您跟踪的任何值已更改时,您才从减速器返回更新的 state。 Changing whether or not you're in edit mode is irrelevant unless you are planning on keeping that state if the user returns to the page.更改您是否处于编辑模式是无关紧要的,除非您计划在用户返回页面时保留该 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM