[AngualrJS NG-redux] Map State and Dispatchers to Redux
In this lesson, we are going to learn how to map our Angular component directly to our application store using the connect method on ngRedux.
In Angular, it is a common technique to bind a controller property directly to the model so that our controllers remain lightweight and operate more as a pass through mechanism than anything else.
Using the connect method, we can accomplish the same effect by not only binding properties directly to the application store, but also binding our controller to our action creators. This allows us to drastically simplify our controllers as we are able to delete a bunch of local controller methods as they become unnecessary because of our mappings.
connect(mapStateToThis, actions)(context):
p1: mapStateToThis: function(state),
p2: actions: object --> ActionsCreators
p3: Context
Basiclly connect do two things:
1. Exports the actions to the controller, so you don't need to do like this:
this.store.dispatch(this.CategoriesActions.getCategoreis());
Instead you can do :
this.getCategoreis();
2. You don't need to subscribe to the store manully anymore, it will automaticlly subscribe to store:
So instead doing like this:
this.unsubscribe = this.store.subscribe(() => { this.categories = this.store.getState().categories; this.currentCategory = this.store.getState().category; });
Now you can do this:
this.unsubscribe = this.store.connect(this.mapStateToThis, actions)(this);
The connect function also return a unsubscribe function which you can call in $onDestroy life cycle.
mapStateToThis:
mapStateToThis(state) { return { categories: state.categories, currentCategory: state.currentCategory }; }
Basiclly it will replace:
this.unsubscribe = this.store.subscribe(() => { this.categories = this.store.getState().categories; this.currentCategory = this.store.getState().category; });
actions:
const actions = Object.assign({}, this.CategoriesActions, this.BookmarksActions); this.unsubscribe = this.store.connect(this.mapStateToThis, actions)(this);
Actions is an object contains the actions creators you need for this controller.
Other benefits:
So for example you have a function called 'deleteBookmark' on the controller. And inside this function, you call 'this.BookmarksActions.deleteBookmark(bookmark)' function.
Two functions' name are the same, then you can get rid of 'deleteBookmark' the whole function.
<button type="button" class="close" ng-click="bookmarksListCtrl.deleteBookmark(bookmark)">×</button>
/* // The whole function can be removed deleteBookmark(bookmark) { this.store.dispatch( this.BookmarksActions.deleteBookmark(bookmark) ) }*/
Becasue connect function already exprots the 'deletedBookmark' function to the ctrl.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2015-11-14 [AngularJS] Use ng-model-options to limit $digest
2015-11-14 [Flux] Component / Views
2015-11-14 [Flux] Stores
2014-11-14 [RSpec] LEVEL 2 CONFIGURATION & MATCHERS
2014-11-14 [RSpec] LEVEL 1: INTRODUCTION
2014-11-14 [AngularJS] Angular 1.3 ngAria - 2
2014-11-14 [AngularJS] Angular1.3 ngAria - 1