博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] Refactor Angular Component State Logic into Directives
阅读量:7037 次
发布时间:2019-06-28

本文共 3041 字,大约阅读时间需要 10 分钟。

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 . 

@Component({  selector: 'toggle',  template: '
',})

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
= new EventEmitter(); setOnState(on: boolean) { this.on = on; this.toggle.emit(this.on); }}

So we can change the usage in app.component.html:

On
Off
Off

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: '
',})export class ToggleButtonComponent { constructor(public toggleProvider: ToggleProviderDirective) {} onClick() { this.toggleProvider.toggle.setOnState(!this.toggleProvider.toggle.on); }}

 

 

 

转载地址:http://dcnal.baihongyu.com/

你可能感兴趣的文章
C语言精简快排代码,带注释
查看>>
Greenplum如何激活、同步、删除Standby恢复原始Master
查看>>
技术往事:改变世界的TCP/IP协议(珍贵多图、手机慎点)
查看>>
减少tcp连接中的time_wait
查看>>
国内一款仅需150M内存的开源JAVA企业网站系统-MiinE
查看>>
基于maven使用IDEA创建springboot多模块项目
查看>>
才上线的第一个iphone app私人相簿(加密保护您的隐私),请大家支持下
查看>>
资源下载地址
查看>>
ngnix下conf通用设置方法(php fastcgi)
查看>>
gnu autotools
查看>>
Sizeof与Strlen的区别与联系
查看>>
Tomcat的三种模式及并发优化
查看>>
Linux系统的Web网站服务
查看>>
oracle的环境配置-oracle10g的安装过程
查看>>
设计模式(创建型)之工厂模式
查看>>
Spring 3: 核心技术之AOP配置
查看>>
我的友情链接
查看>>
中小企业是否需要邮件服务器系统
查看>>
关机命令
查看>>
禁止浏览器缓存的响应头和定时刷新
查看>>