4

How do I concatenate strings in Angular HTML Input? The following is creating dropdown. What we want is have txtField be concatenation of addressCode + ' ' + addressDescription, trying placing into input, did not work. Looking for way in html first, before trying in angular typescript if possible.

Original:

<app-drop-down 
  [listItems]="'addressList'"
  [txtField]="'addressCode'"
  [txtValue]="'addressId'"
</app-drop-down>

Attempt

<app-drop-down 
  [listItems]="'addressList'"
  [txtField]="'addressCode' + 'addressDescription'"
  [txtValue]="'addressId'"
</app-drop-down>
9
  • concatenate in the .ts file and pass the same Commented Dec 9, 2019 at 5:32
  • is addressCode and addressDescription are the value of input field? Commented Dec 9, 2019 at 5:33
  • 1
    Its working here. Can not reproduce the same. Check here stackblitz.com/edit/… Commented Dec 9, 2019 at 5:35
  • hi @HarunurRashid for some reason no, its going off values in an array, along with wrapper on material design angular input Commented Dec 9, 2019 at 5:40
  • addressCode and addressDescription are from input? Commented Dec 9, 2019 at 5:41

2 Answers 2

2

You can try like this -

<app-drop-down 
  [listItems]="addressList"
  txtField="{{addressCode + addressDescription}}"
  [txtValue]="addressId"
</app-drop-down>

But following way also should work , as you mentioned in your question.

<app-drop-down 
  [listItems]="addressList"
  [txtField]="addressCode + addressDescription"
  [txtValue]="addressId"
</app-drop-down>

Here is the demo - https://stackblitz.com/edit/angular-59242999 Hope this helps, Let me know still if it does not work for you.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, I see why now, can you create an addendum to your answer, I had single quotes for values in question, thanks
you mean addressCode and addressDescription are strings ? or variables ?
0

You can create simply a function

in .ts

concateInput(str1, str2){
return str1.concat(str2);
}

and call it from .html

<app-drop-down 
  [listItems]="addressList"
  [txtField]="concateInput(addressCode, addressDescription)"
  [txtValue]="addressId"
</app-drop-down>

1 Comment

when a user clicks on dropdown add that element into the array and pass that array to convert into a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.