如何从指令 add/remove class
How to add/remove class from directive
使用自定义指令,您如何根据特定条件 add/remove 在宿主元素上 class?
示例:
@Directive({
selector: '[customDirective]'
})
export class CustomDirective {
constructor(service: SomService) {
// code to add class
if (service.someCondition()) {
// code to remove class
}
}
}
如果你不想使用 ngClass
指令(提示:你可以将一个函数传递给 [ngClass]="myClasses()"
如果它会在你的模板中内联混乱)你可以使用Renderer2
为其添加一个或多个类:
export class CustomDirective {
constructor(private renderer: Renderer2,
private elementRef: ElementRef,
service: SomService) {
}
addClass(className: string, element: any) {
this.renderer.addClass(element, className);
// or use the host element directly
// this.renderer.addClass(this.elementRef.nativeElement, className);
}
removeClass(className: string, element: any) {
this.renderer.removeClass(element, className);
}
}
export class CustomDirective {
classname:string = "magenta";
constructor(private renderer: Renderer2,
private elementRef: ElementRef,
service: SomService) {
}
addClass(className: string, element: any) {
// make sure you declare classname in your main style.css
this.renderer.addClass(this.elementRef.nativeElement, className);
}
removeClass(className: string, element: any) {
this.renderer.removeClass(this.elementRef.nativeElement,className);
}
}
当您在 Angular 中使用指令时,您需要使用 @HostBinding
,并绑定到 class.your-class
以便能够 add/remove 您的 [=26] =] 基于谓词。您无需在 Renderer2
中进行 DI 即可有效 add/remove classes.
例如,当使用 Bootstrap 和 Reactive Forms 时,您想要指示有效或无效的表单字段,您可以执行以下操作:
import { Directive, Self, HostBinding, Input } from '@angular/core';
import { NgControl } from '@angular/forms';
@Directive({
selector: '[appCheckFormFieldValidity]'
})
export class CheckFormFieldValidity{
@Input() public class: string;
constructor(
@Self() private ngControl: NgControl
) { }
@HostBinding('class.is-valid')
public get isValid(): boolean {
return this.valid;
}
@HostBinding('class.is-invalid')
public get isInvalid(): boolean {
return this.invalid;
}
public get valid(): boolean {
return this.ngControl.valid &&
(this.ngControl.dirty || this.ngControl.touched);
}
public get invalid(): boolean {
return !this.ngControl.pending &&
!this.ngControl.valid &&
(this.ngControl.touched || this.ngControl.dirty);
}
}
这不是一个严谨的例子,但它说明了@HostBinding
的用法,我在StackBlitz
中创建了这个例子
在下拉菜单上打开和关闭切换的指令示例
import { Directive, ElementRef, Renderer2, HostListener, HostBinding } from '@angular/core';
@Directive({
selector: '[appDropDown]',
})
export class DropsownDirective{
@HostBinding('class.open') isopen = false;
@HostListener('mouseenter') onMouseEnter(){
this.isopen = !this.isopen;
}
@HostListener('mouseleave') onMouseLeave(){
this.isopen = !this.isopen;
}
}
组件添加指令appDropDown
<div class="col-xs-12">
<div class="btn-group" appDropDown>
<button class="btn btn-primary dropdown-toggle">
Manage Movie <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="#">To watching List</a></li>
<li><a href="#">Edit Movie</a></li>
<li><a href="#">Delete Movie</a></li>
</ul>
</div>
确保在
@NgModule 声明
使用自定义指令,您如何根据特定条件 add/remove 在宿主元素上 class?
示例:
@Directive({
selector: '[customDirective]'
})
export class CustomDirective {
constructor(service: SomService) {
// code to add class
if (service.someCondition()) {
// code to remove class
}
}
}
如果你不想使用 ngClass
指令(提示:你可以将一个函数传递给 [ngClass]="myClasses()"
如果它会在你的模板中内联混乱)你可以使用Renderer2
为其添加一个或多个类:
export class CustomDirective {
constructor(private renderer: Renderer2,
private elementRef: ElementRef,
service: SomService) {
}
addClass(className: string, element: any) {
this.renderer.addClass(element, className);
// or use the host element directly
// this.renderer.addClass(this.elementRef.nativeElement, className);
}
removeClass(className: string, element: any) {
this.renderer.removeClass(element, className);
}
}
export class CustomDirective {
classname:string = "magenta";
constructor(private renderer: Renderer2,
private elementRef: ElementRef,
service: SomService) {
}
addClass(className: string, element: any) {
// make sure you declare classname in your main style.css
this.renderer.addClass(this.elementRef.nativeElement, className);
}
removeClass(className: string, element: any) {
this.renderer.removeClass(this.elementRef.nativeElement,className);
}
}
当您在 Angular 中使用指令时,您需要使用 @HostBinding
,并绑定到 class.your-class
以便能够 add/remove 您的 [=26] =] 基于谓词。您无需在 Renderer2
中进行 DI 即可有效 add/remove classes.
例如,当使用 Bootstrap 和 Reactive Forms 时,您想要指示有效或无效的表单字段,您可以执行以下操作:
import { Directive, Self, HostBinding, Input } from '@angular/core';
import { NgControl } from '@angular/forms';
@Directive({
selector: '[appCheckFormFieldValidity]'
})
export class CheckFormFieldValidity{
@Input() public class: string;
constructor(
@Self() private ngControl: NgControl
) { }
@HostBinding('class.is-valid')
public get isValid(): boolean {
return this.valid;
}
@HostBinding('class.is-invalid')
public get isInvalid(): boolean {
return this.invalid;
}
public get valid(): boolean {
return this.ngControl.valid &&
(this.ngControl.dirty || this.ngControl.touched);
}
public get invalid(): boolean {
return !this.ngControl.pending &&
!this.ngControl.valid &&
(this.ngControl.touched || this.ngControl.dirty);
}
}
这不是一个严谨的例子,但它说明了@HostBinding
的用法,我在StackBlitz
在下拉菜单上打开和关闭切换的指令示例
import { Directive, ElementRef, Renderer2, HostListener, HostBinding } from '@angular/core';
@Directive({
selector: '[appDropDown]',
})
export class DropsownDirective{
@HostBinding('class.open') isopen = false;
@HostListener('mouseenter') onMouseEnter(){
this.isopen = !this.isopen;
}
@HostListener('mouseleave') onMouseLeave(){
this.isopen = !this.isopen;
}
}
组件添加指令appDropDown
<div class="col-xs-12">
<div class="btn-group" appDropDown>
<button class="btn btn-primary dropdown-toggle">
Manage Movie <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="#">To watching List</a></li>
<li><a href="#">Edit Movie</a></li>
<li><a href="#">Delete Movie</a></li>
</ul>
</div>
确保在 @NgModule 声明