zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Angular] Modify User Provided UI with Angular Content Directives

AngularUI with user content modify provided directives
2023-09-14 09:00:49 时间

If we’re going to make our toggle accessible, we’ll need to apply certain aria attributes to the control that does the toggling. Instead of making the parent component handle that, we can provide a toggler directive that automatically applies the needed attributes. Content directives allow the user to tell the UI component where to make those modifications.

 

<switch toggler [on]="on" (click)="fns.toggle()">
</switch>

 

We added a toggler directive to share all the common attrs:

import { Directive, HostBinding, Input } from '@angular/core';

@Directive({
  selector: '[toggler]'
})
export class TogglerDirective {
  @HostBinding('attr.role') role = 'switch';

  @HostBinding('attr.aria-checked') @Input() on;
  
  constructor() { }

}