[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自己特有的选项 ...
随机推荐
- IoC简介
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来降低程序代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency Injecti ...
- sleep 和wait的差别
基本的差别 1.sleep 是Thread 类的方法,wait 是Object类中定义的方法 2.sleep()方法可以在任何地方使用 3.wait()方法只能在synchronized方法中使用,或 ...
- Dubbo框架的说明
说实话,自己现在做的项目中有用到dubbo,但是我所负责的那一个模块,并没有涉及到dubbo,想学习一下dubbo,之前是没有学习完,这次继续... 一.背景知识总结 二.服务治理 三.Dubbo架构 ...
- Go:类型断言
一.基本介绍 类型断言:由于接口是一般类型,不知道具体类型,如果要转成具体类型,就需要使用类型断言. 如果希望将一个空接口类型重新转换成对应的类型,那么需要使用类型断言,能转换成功是因为这个空接口原先 ...
- xenserver tools 安装
mkdir -p /mnt/xtools mount /dev/cdrom /mnt/xtools cd /mnt/xtools/Linux/ ./install.sh -n init 6
- 企业级监控nagios实践
nagios 监控服务应用指南 小区:视频监控,保安 企业工作中为什么要部署监控系统 监控系统相当于哨兵的作用,监控几百台上千台服务器,监控系统非常重要. 监控系统都需要监控 1. 本地资源:负载up ...
- Could not resolve dependencies for project com.shadow:shlang:jar:1.0-SNAPSHOT:
maven打包项目出现缺少jar包错误 如果是将本地引用的jar包放在了lib目录下并通过下面方式引入 解决方案为 <dependency> <groupId>com.o ...
- 3.3.3 使用 join 连接字段
join 命令可以将多个文件结合在一起,每个文件里的每条记录,都共享一个键值(key),键值指的是记录中的主字段,通常会是用户名称.个人姓氏.员工编号之类的数据.举例来说,两个文件,一个列出所 ...
- [WPF自定义控件]使用WindowChrome自定义Window Style
1. 为什么要自定义Window 对稍微有点规模的桌面软件来说自定义的Window几乎是标配了,一来设计师总是克制不住自己想想软件更个性化,为了UI的和谐修改Window也是必要的:二来多一行的空间可 ...
- BootStrap学习01框架搭建
中文文档:https://v3.bootcss.com/css/ 开发工具 WebStorm 一.新建项目08bootstrap 引入bootstrap-3.3.7,引入jQuery,引入holder ...