[Angular] Expose Angular Component Logic Using State Reducers
A component author has no way of knowing which state changes a consumer will want to override, but state reducers handle this problem by allowing the parent component to override any state change.
In Short, we want to have a way to override the component's internal state from outside. The reason for that is there maybe some requirements from the users who want to override component internal state for whatever reason.
The state reducer can accomplish this task, so what is state reducer? It is just a fansic name form some smart developers. State reducer is a function which two two params, one is old state, another one is changes going to be happen, return value is the new state.
export type ToggleStateReducer =
(state: ToggleState, changes: Partial<ToggleState>)
=> ToggleState;
So inside toggle.component.ts, we accept an @Input() stateReducer:
@Input() stateReducer: ToggleStateReducer =
(state, changes) => ({ ...state, ...changes });
Whenenver we need to change toggle component internal state, we call stateReducer:
setOnState(on: boolean) {
const oldState = { on: this.on };
const newState = this.stateReducer(oldState, { on });
if (oldState !== newState) {
this.on = newState.on;
this.toggled.emit(this.on);
}
}
That's all what we need to for component part. Just make stateReducer as a input, call each time we need to udpate our internal state, we call thought stateReducer to get new state.
So now, from the consumer component, we can control the state update:
// app.component.ts: stateReducer = (state: ToggleState, changes: Partial<ToggleState>) => {
if (this.timesClicked > ) {
return state;
}
if (changes.on !== undefined) {
this.timesClicked = this.timesClicked + ;
}
return { ...state, ...changes };
}
<toggle (toggled)="log('toggle', $event)" [stateReducer]="stateReducer">
<ng-template let-on="on" let-fns="fns">
<switch [on]="on" toggler (click)="fns.toggle()">
</switch>
</ng-template>
</toggle>
[Angular] Expose Angular Component Logic Using State Reducers的更多相关文章
- Exploring the Angular 1.5 .component() method
Angular 1.5 introduced the .component() helper method, which is much simpler than the.directive() de ...
- [Angular] Use Angular components in AngularJS applications with Angular Elements
When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...
- angular 2 angular quick start Could not find HammerJS
Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...
- ASP.NET Core 2.1 Web API + Identity Server 4 + Angular 6 + Angular Material 实战小项目视频
视频简介 ASP.NET Core Web API + Angular 6的教学视频 我是后端开发人员, 前端的Angular部分讲的比较差一些, 可以直接看代码!!!! 这是一个小项目的实战视频, ...
- [Angular] Refactor Angular Component State Logic into Directives
Allow the base toggle to be a tag (<toggle>) or attribute (<div toggle>). The <toggle ...
- [Angular] Two ways to create Angular Animation, using animation() or using state()
We have two blocks to show to difference ways to do animation in Angular: <button (click)="t ...
- Angular(二) - 组件Component
1. 组件Component示例 2. Component常用的几个选项 3. Component全部的选项 3.1 继承自@Directive装饰器的选项 3.2 @Component自己特有的选项 ...
- [Angular 2] Passing Template Input Values to Reducers
Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...
- [Angular] Use Angular’s @HostBinding and :host(...) to add styling to the component itself
One thing that we can do is to add styles directly to HTML elements that live within our component. ...
随机推荐
- chrome ubuntu启动不了
安装好后,点击图标没反应 用命令行运行chrome并显示log:google-chrome --enable-logging=stderr --log-level=4 报错如下: 解决:安装更高版本的 ...
- 解决Invalid bound statement (not found)(Mybatis的Mapper绑定问题)
一.问题描述 使用mybatis的项目在本地可以正常运行,但当使用maven或Jenkins打包部署到服务器上时出现了绑定错误,异常信息为: org.apache.ibatis.binding.Bin ...
- 微信小程序:errcode=40029和invalid code, hints: [ req_id: VyLhYa0451hb31 ]
问题: 后台用小程序返回的code请求微信服务器换取session_key和openid,返回错误状态码40029 解决问题 当前小程序绑定的appid和请求微信服务器所带的appid参数不一致导致的 ...
- Sqlserver添加加字段、删除字段、修改字段类型、修改字段名、修改字段默认值
参考:https://www.cnblogs.com/pangpanghuan/p/6432331.html 初始化表: --.添加字段 --1.1.为null alter table DataTab ...
- 【亲测可行】Dev c++调试、运行报错解决方法总结
一.编译后 0错误 0警告,但是开始出现‘‘停止运行’’或者进行输入时出现‘‘停止运行’’ 可能的原因: 结构体指针为空,但调用了其成员. 有些scanf语句中忘记添加取址符. 无法跳出递归. 二. ...
- Visual C++中error spawning cl.exe错误的两种解决方法.
可能很多人在安装VC 6.0后有过点击“Compile”或者“Build”后被出现的 “Compiling... ,Error spawning cl.exe”错误提示给郁闷过.很多人的 选择是重装, ...
- 在single模式修改密码
当前是在CentOS 6.5 的命令行模式下 开机后在这个界面按随意键 接着来到新界面中,在当前选项中按下e键: 接着来到新界面,选择第二个,按下e键: 在这里我们输入single接着回车: 接着回到 ...
- API对接中经常会出现的签名获取,这只是某一种,仅供给有需要的人参考
要求: 1.对所有传入参数(含系统参数和接口参数)按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的格式.(即 key1=value1&key2=value2…)拼接 ...
- HTML5新增的主体元素article、section、nav、aside、time元素和pubdate属性
article artticle元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容.它可以是一篇博客或者报刊中的文章,一篇论坛帖子,一段用户评论或者独立的插件或其他任何独立的内容. ...
- STM32开发环境--使用MDK建立一个工程
STM32开发环境--使用MDK建立一个工程 该工程模版是基于库函数基础制作而成,其中有借鉴相关资料.虽然工程模版一旦建立,以后任何项目只需套用即可,但考虑到长时间不使用,在将来某天可能会突然用到,再 ...