Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angular 2 how to put array string elements into template element value-attribute?

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?

like image 288
brinch Avatar asked Feb 15 '17 22:02

brinch


1 Answers

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

<input name='user-tag' placeholder='write some tags' value='{{ myArray.join(', ') }}'>
like image 79
Adam Avatar answered Nov 14 '22 22:11

Adam