[Angular] Refactor Angular Component State Logic into Directives
Allow the base toggle to be a tag (<toggle>) or attribute (<div toggle>). The <toggle> component has become less opinionated about the view, but has now taken on some responsibilities managing state. We’ll decouple the state management piece by moving it into the toggleProvider directive. The toggleProvider directive provides state for all the <toggle-off>, <toggle-on> and <toggle-button> components inside it.
For toggle component we made in previous post.
@Component({
selector: 'toggle',
template: '<ng-content></ng-content>',
})
As you can see, it use 'ng-content' inside template which means that it doesn't actually needs a template, we can consider ToggleComponent as a directive.
So we can modifiy ToggleComponet to ToggleDirective:
import { Directive, Input, Output, EventEmitter } from '@angular/core'; @Directive({
selector: 'toggle, [toggle]'
})
export class ToggleDirective {
@Input() on: boolean;
@Output() toggle: EventEmitter<boolean> = new EventEmitter(); setOnState(on: boolean) {
this.on = on;
this.toggle.emit(this.on);
}
}
So we can change the usage in app.component.html:
<div toggle (toggle)="onToggle($event)">
<toggle-on>On</toggle-on>
<toggle-off>Off</toggle-off>
<toggle-off>Off</toggle-off>
<other-component></other-component>
<toggle-button></toggle-button>
</div>
Then change all the dependencies injection for toggle-on/off/button component, the application should still works.
One problem for the current directive implementations is that each toggle directives are isolated:
Most of times, isolated directives are OK, but in some cases, if you want to share the state between two or more directives. You have to do some extra works.
Write ToggleProviderDirective for reference ToggleDirective.
state <-- ToggleDirective <-- ToggleProviderDirective
So ToggleDirective is managing the state, if we want to share the state, we create ToggleProviderDirective, it takes ToggleDirective as input, so that we can share one ToggleDirective thoughts multi directives.
import { Directive, Input, Output, Host, OnChanges, SimpleChanges, Optional } from '@angular/core';
import {ToggleDirective} from './toggle.directive'; @Directive({
exportAs: 'toggleProvider',
selector: 'toggle, [toggle], [toggleProvider]',
})
export class ToggleProviderDirective implements OnChanges { @Input() toggleProvider: ToggleDirective; toggle: ToggleDirective = this.toggleDirective; constructor(
// Reference the toggle directive on the same host element
@Host() @Optional() private toggleDirective: ToggleDirective
) { } ngOnChanges(changes: SimpleChanges) {
const {toggleProvider} = changes;
if (toggleProvider) {
this.toggle = this.toggleProvider || this.toggleDirective;
}
}
}
Also need to change the reference for toggle-on/off/button:
import { Component } from '@angular/core'; import { ToggleProviderDirective } from './toggle.toggleProvider.directive'; @Component({
selector: 'toggle-button',
template: '<switch [on]="toggleProvider.toggle.on" (click)="onClick()" ></switch>',
})
export class ToggleButtonComponent {
constructor(public toggleProvider: ToggleProviderDirective) {} onClick() {
this.toggleProvider.toggle.setOnState(!this.toggleProvider.toggle.on);
}
}
[Angular] Refactor Angular Component State Logic into Directives的更多相关文章
- Exploring the Angular 1.5 .component() method
Angular 1.5 introduced the .component() helper method, which is much simpler than the.directive() de ...
- [Angular 2] Exposing component properties to the template
Showing you how you can expose properties on your Controller to access them using #refs inside of yo ...
- [React] Update Component State in React With Ramda Lenses
In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...
- [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部分讲的比较差一些, 可以直接看代码!!!! 这是一个小项目的实战视频, ...
- React 手稿 - Component state
Component state 实例: import React, { PureComponent } from 'react'; export default class extends PureC ...
- [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 s ...
- Angular(二) - 组件Component
1. 组件Component示例 2. Component常用的几个选项 3. Component全部的选项 3.1 继承自@Directive装饰器的选项 3.2 @Component自己特有的选项 ...
随机推荐
- pc端移动端兼容的大图轮播
body, html { width: 100%; } * { margin:; padding:; list-style: none; } .haha { list-style-type: none ...
- POJ-2442-Sequence(二叉堆)
POJ-2442 Description Given m sequences, each contains n non-negative integer. Now we may select one ...
- XML 解析 & 特殊字符报错
在xml文件中,有一些符号是具有特殊意义的,如果直接使用会导致xml解析报错,为了避免错误,我们需要将特殊的字符使用其对应的转义实体进行操作.这些字符如下 < == < > = ...
- 16. PLUGINS
16. PLUGINS PLUGINS表提供有关服务器插件的信息. PLUGINS表有以下列: PLUGIN_NAME :用于在诸如INSTALL PLUGIN和UNINSTALL PLUGIN之类的 ...
- 第十章:C++标准模板库
主要内容: 1.泛型程序设计 2.与STL有关的概念和术语 3.STL的容器 4.迭代器 5.STL的算法 6.函数对象 暂时略,内容有点多,而且也很重要!但我看完了,日后补上.
- 条款5:了解C++默默编写并调用哪些函数(Know what functions C++ silently writes and calls)
1.default costructor / copy constructor / copy assignment 者三者的区别? 特别是copy constructor & copy as ...
- python str.format 中文对齐的细节问题
写了一个练手的爬虫...在输出的时候出现了让人很不愉♂悦的问题 像这样: 令人十分难受啊! #----------------------------------------------------- ...
- Activiti数据表
--1:资源库流程规则表SELECT * FROM JEESITE.act_re_deployment --部署信息表SELECT * FROM JEESITE.act_re_model --流程设计 ...
- pwnable.kr cmd2之write up
来看一下源代码: #include <stdio.h> #include <string.h> int filter(char* cmd){ ; r += strstr(cmd ...
- 关于程序计数器(PC)和条件控制转移 引起的性能差异
关于PC(程序计数器) 冯 ·诺伊曼计算机体系结构的主要内容之一就是“程序预存储,计算机自动执行”! 处理器要执行的程序(指令序列)都是以二进制代码序列方式预存储在计算机的存储器中,处理器将这些代码逐 ...