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

本文共 3095 字,大约阅读时间需要 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); }}

 

 

 

转载于:https://www.cnblogs.com/Answer1215/p/9764179.html

你可能感兴趣的文章
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>
IO流入门-第十章-DataInputStream_DataOutputStream
查看>>
DRF的分页
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
python:open/文件操作
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>
MySQL批量SQL插入性能优化
查看>>
定义列属性:null,default,PK,auto_increment
查看>>