File

integration-libs/cds/src/merchandising/cms-components/directives/attributes/attributes.directive.ts

Implements

OnChanges

Metadata

Selector [cxAttributes]

Index

Properties
Methods
Inputs
Accessors

Constructor

constructor(renderer: Renderer2, elementRef: ElementRef)
Parameters :
Name Type Optional
renderer Renderer2 No
elementRef ElementRef No

Inputs

cxAttributes
Type : literal type
cxAttributesNamePrefix
Type : string

Methods

ngOnChanges
ngOnChanges()
Returns : void

Properties

Private _attributesNamePrefix
Type : string
cxAttributes
Type : literal type
Decorators :
@Input()

Accessors

cxAttributesNamePrefix
setcxAttributesNamePrefix(attributesNamePrefix: string)
Parameters :
Name Type Optional
attributesNamePrefix string No
Returns : void
import {
  Directive,
  ElementRef,
  Input,
  OnChanges,
  Renderer2,
} from '@angular/core';

@Directive({
  selector: '[cxAttributes]',
})
export class AttributesDirective implements OnChanges {
  @Input() cxAttributes: { [attribute: string]: any };

  private _attributesNamePrefix: string;
  @Input() set cxAttributesNamePrefix(attributesNamePrefix: string) {
    this._attributesNamePrefix = attributesNamePrefix;
  }

  constructor(private renderer: Renderer2, private elementRef: ElementRef) {}

  ngOnChanges(): void {
    if (this.cxAttributes) {
      for (const attributeName in this.cxAttributes) {
        if (this.cxAttributes.hasOwnProperty(attributeName)) {
          const attributeValue = this.cxAttributes[attributeName];
          if (attributeValue) {
            const _attributeName = this._attributesNamePrefix
              ? `${this._attributesNamePrefix}-${attributeName}`
              : attributeName;
            this.renderer.setAttribute(
              this.elementRef.nativeElement,
              _attributeName,
              attributeValue
            );
          }
        }
      }
    }
  }
}

result-matching ""

    No results matching ""