1- import { Directive , ElementRef , Input , OnInit , Renderer2 } from '@angular/core' ;
1+ import { Directive , ElementRef , Input , OnInit , Renderer2 } from '@angular/core' ;
22
33@Directive ( {
44 selector : '[cHtmlAttr]' ,
@@ -7,8 +7,7 @@ import {Directive, ElementRef, Input, OnInit, Renderer2} from '@angular/core';
77} )
88export class HtmlAttributesDirective implements OnInit {
99
10- // @ts -ignore
11- @Input ( ) cHtmlAttr : { [ key : string ] : any } ;
10+ @Input ( ) cHtmlAttr ?: { [ key : string ] : any } ;
1211
1312 constructor (
1413 private renderer : Renderer2 ,
@@ -18,7 +17,7 @@ export class HtmlAttributesDirective implements OnInit {
1817 ngOnInit ( ) : void {
1918 const attribs = this . cHtmlAttr ;
2019 for ( const attr in attribs ) {
21- if ( attr === 'style' && typeof ( attribs [ attr ] ) === 'object' ) {
20+ if ( attr === 'style' && typeof ( attribs [ attr ] ) === 'object' ) {
2221 this . setStyle ( attribs [ attr ] ) ;
2322 } else if ( attr === 'class' ) {
2423 this . addClass ( attribs [ attr ] ) ;
@@ -36,16 +35,16 @@ export class HtmlAttributesDirective implements OnInit {
3635 }
3736 }
3837
39- private addClass ( classes : string ) : void {
38+ private addClass ( classes : string | string [ ] ) : void {
4039 const classArray = ( Array . isArray ( classes ) ? classes : classes . split ( ' ' ) ) ;
4140 classArray . filter ( ( element ) => element . length > 0 ) . forEach ( element => {
42- this . renderer . addClass ( this . el . nativeElement , element ) ;
41+ this . renderer . addClass ( this . el . nativeElement , element ) ;
4342 } ) ;
4443 }
4544
4645 private setAttrib ( key : string , value : string | null ) : void {
4746 value !== null ?
48- this . renderer . setAttribute ( this . el . nativeElement , key , value ) :
47+ this . renderer . setAttribute ( this . el . nativeElement , key , value ) :
4948 this . renderer . removeAttribute ( this . el . nativeElement , key ) ;
5049 }
5150}
0 commit comments