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,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); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。