integration-libs/cds/src/merchandising/cms-components/directives/attributes/attributes.directive.ts
| Selector | [cxAttributes] |
Properties |
|
Methods |
Inputs |
Accessors |
constructor(renderer: Renderer2, elementRef: ElementRef)
|
|||||||||
|
Parameters :
|
| cxAttributes | |
Type : literal type
|
|
| cxAttributesNamePrefix | |
Type : string
|
|
| ngOnChanges |
ngOnChanges()
|
|
Returns :
void
|
| Private _attributesNamePrefix |
Type : string
|
| cxAttributes |
Type : literal type
|
Decorators :
@Input()
|
| cxAttributesNamePrefix | ||||||
setcxAttributesNamePrefix(attributesNamePrefix: string)
|
||||||
|
Parameters :
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
);
}
}
}
}
}
}