如何使用ng-model更新Redux状态



我有这个HTML:

<input ng-model="state.input.name">
<input ng-model="state.input.password">

我有一个Redux商店。我希望每次用户在框中输入值时,我想运行update store Redux state:

,而不是像Angular那样只改变值

像这样:

store.setState(_.extend({},store.getState(),{input:state.input})

我可以用ng-change来做,关键是我想让它对任何ng-model都能自动工作。我还想知道state.input.name从state中得到的值。对于只读元素,没有问题:

<div ng-bind="{{store.getState().input.name}}">

但是对于读/写,我不能自动连接到存储

我想也许要拦截Angular.apply * Angular.dispatch方法或创建一个ng-model指令,添加此功能。

我发现了一个非常简单的方法来做到这一点,通过使用Angular watch方法:

$rootScope.watch('state.input',function(){
    store.setState({input:$rootScope.state})
)

最新更新