9

I have this array called myArray in my Angular 2 component class:

@Component({
    templateUrl: 'my.component.html'
})

export class MyComponent {
    private myArray: Array<string>;
    ...
}

In my.component.html I have a single input element:

<input placeholder='write some tags' value=''>

What I want is to have the string elements of myArray inserted into the value attribute of the input element. The strings should be comma separated. Something like:

<input placeholder='write some tags' value='apple, orange, banana'>

I tried:

<input name='user-tag' placeholder='write some tags' value='*ngFor="let e of myArray"{{e}}'>

But that produced an error.

How can I do this?

1 Answer 1

18

No need to use *ngFor, try using Array.join().

<input name='user-tag' placeholder='write some tags' value='{{ myArray.join(', ') }}'>
Sign up to request clarification or add additional context in comments.

2 Comments

I would suggest you to create a filter to do this kind of thing so you will have a single point of ref and when some improvement is needed you won't need to change more than one place.
Absolutely, a pipe would be ideal for what you're describing, but I didn't want to over architect the answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.