1
- import { Directive , ElementRef , Input , OnInit , Renderer2 } from '@angular/core' ;
1
+ import { Directive , ElementRef , Input , OnInit , Renderer2 } from '@angular/core' ;
2
2
3
3
@Directive ( {
4
4
selector : '[cHtmlAttr]' ,
@@ -7,8 +7,7 @@ import {Directive, ElementRef, Input, OnInit, Renderer2} from '@angular/core';
7
7
} )
8
8
export class HtmlAttributesDirective implements OnInit {
9
9
10
- // @ts -ignore
11
- @Input ( ) cHtmlAttr : { [ key : string ] : any } ;
10
+ @Input ( ) cHtmlAttr ?: { [ key : string ] : any } ;
12
11
13
12
constructor (
14
13
private renderer : Renderer2 ,
@@ -18,7 +17,7 @@ export class HtmlAttributesDirective implements OnInit {
18
17
ngOnInit ( ) : void {
19
18
const attribs = this . cHtmlAttr ;
20
19
for ( const attr in attribs ) {
21
- if ( attr === 'style' && typeof ( attribs [ attr ] ) === 'object' ) {
20
+ if ( attr === 'style' && typeof ( attribs [ attr ] ) === 'object' ) {
22
21
this . setStyle ( attribs [ attr ] ) ;
23
22
} else if ( attr === 'class' ) {
24
23
this . addClass ( attribs [ attr ] ) ;
@@ -36,16 +35,16 @@ export class HtmlAttributesDirective implements OnInit {
36
35
}
37
36
}
38
37
39
- private addClass ( classes : string ) : void {
38
+ private addClass ( classes : string | string [ ] ) : void {
40
39
const classArray = ( Array . isArray ( classes ) ? classes : classes . split ( ' ' ) ) ;
41
40
classArray . filter ( ( element ) => element . length > 0 ) . forEach ( element => {
42
- this . renderer . addClass ( this . el . nativeElement , element ) ;
41
+ this . renderer . addClass ( this . el . nativeElement , element ) ;
43
42
} ) ;
44
43
}
45
44
46
45
private setAttrib ( key : string , value : string | null ) : void {
47
46
value !== null ?
48
- this . renderer . setAttribute ( this . el . nativeElement , key , value ) :
47
+ this . renderer . setAttribute ( this . el . nativeElement , key , value ) :
49
48
this . renderer . removeAttribute ( this . el . nativeElement , key ) ;
50
49
}
51
50
}
0 commit comments