I have written a custom dropdown component and I want to use inside it a template
tag to style the output. For example:
<cstm-dropdown [model]="incident.sensor" [options]="sensors" [id]="'id'">
<template>
{{option.name | localName}}
</template>
</cstm-dropdown>
this should display the name
property of the options with some pipe transformation on it.
My component is :
import { Component, Input, OnChanges } from '@angular/core';
@Component({
selector: 'cstm-dropdown',
template: `
<select [ngModel]="selectedOption" (ngModelChange)="onModelChange($event)" >
<option *ngFor="let option of options" [ngValue]="option">
<!-- The template should be rendered here -->
</option>
</select>
`
})
export class CustomDropdownComponent implements OnChanges {
@Input() model: any;
selectedOption: any;
@Input() options: any[];
@Input() id: any;
@Input() idProperties: any[];
ngOnChanges() {
if (!this.identifyByProperty()) {
this.identifyByProperties();
}
}
onModelChange(event) {
for (const key of Object.keys(event)) {
this.model[key] = event[key];
}
}
identifyByProperty(): boolean {
if (!this.id) {
return false;
}
for (const option of this.options) {
if (this.model[this.id] === option[this.id]) {
this.selectedOption = option;
return true;
}
}
return false;
}
identifyByProperties(): boolean {
// if the array isn't passed
if (!this.idProperties) {
return false;
}
// if the passed array is empty
if (!this.idProperties.length) {
return false;
}
for (const option of this.options) {
if (this.arePropertiesIdentical(option)) {
this.selectedOption = option;
return true;
}
}
return false;
}
arePropertiesIdentical(option: any): boolean {
for (const prop of this.idProperties) {
if (this.model[prop] !== option[prop]) {
return false;
}
}
return true;
}
}
I read that I should use TemplateRef
, but couldn't fimd any tutorials on how to do the templating with it. Any help is welcomed :)