If you really want to achieve it via directive, its possible. Please refer to directive code below :-
import {
Directive,
Input,
NgIterable,
OnChanges,
OnInit,
Renderer2,
TemplateRef,
ViewContainerRef
} from "@angular/core";
@Directive({
selector: "[ngJoin]"
})
export class NgJoinDirective<T, U extends NgIterable<T> = NgIterable<T>>
implements OnInit, OnChanges {
@Input() ngJoinOf: any[];
@Input() ngJoinSeparator: string = ",";
constructor(
private _viewContainer: ViewContainerRef,
private _template: TemplateRef<any>,
private renderer2: Renderer2
) {}
ngOnChanges() {
this.createText();
}
ngOnInit() {
this.createText();
}
createText() {
if (this.ngJoinOf) {
this._viewContainer.clear();
console.log(this.ngJoinOf);
const container = this._template.createEmbeddedView({});
const parentNode = container.rootNodes[0];
const textNode = this.renderer2.createText(
this.ngJoinOf.join(this.ngJoinSeparator)
);
this.renderer2.insertBefore(parentNode, textNode, parentNode.firstChild);
this._viewContainer.insert(container);
}
}
}
and used in html like below :-
<p *ngJoin="let item of arr;separator: '/'">
if you will not give separator it will take , as default separator.
Working Stackblitz :-
https://stackblitz.com/edit/angular-rumkzc?file=src/app/app.component.html
If you are just looking to print array via join in template using a separator, pipe will be a ideal choice instead of directive. Such a pipe is provided by a library.
https://github.com/fknop/angular-pipes
The pipe name is join.
Note :- I have not handled, any edge cases, you might want to handle.
*ngFor, but with a separator between (like a.join). Commonly, a separator is a simple string, like a comma. I'd like to be able to write<x *ngJoin="let item of items; separator: ','"></x>instead of<x *ngJoin="let item of items; separator: separatorTpl"></x> <ng-template #separatorTpl>,</ng-template>. Also I'd like to provide comma as the default value of@Input() separator. If its type is aTemplateRef, there's no way of giving it a default value (impossible to create TemplateRef from code -- or is it?).