I would like ignore the initial value undefined of string variable, I'm trying concatenate words separate by comma but the first concatenation is an undefined. I can not use array of string.
Can it ignore undefined value?
MyComponent.ts
addTag(tag: any){
this.tags = `${this.asunto.etiquetas}, ${tag}`;
(<HTMLInputElement>document.getElementById("tags")).value = '';
}
View of my angular component
You should put the etiquetas when available, otherwise an empty string. Since you can write normal Javascript inside that template literal:
addTag(tag: any){
this.tags = `${this.asunto.etiquetas ? `${this.asunto.etiquetas}, ` : ""} ${tag}`;
(<HTMLInputElement>document.getElementById("tags")).value = '';
}
A better version would be to just use a variable
addTag(tag: any){
const etiqueta = this.asunto.etiquetas ? `${this.asunto.etiquetas}, `} : "";
this.tags = `${etiqueta}${tag}`;
(<HTMLInputElement>document.getElementById("tags")).value = '';
}