I have a template
<ng-container *ngFor="let text of texts[i]; let j = index">{{text}}
<input type="text">
</ng-container>
I also have an array myWords
myWords = ['orange', 'blue', 'green'];
How could I insert these words like a placeholder, but only in special case, like
this.app-part
I have 3 parts in my app, but I would like to have a placeholder (words from myWords
) only in 3rd part, and if I have 1st or 2nd part - I don't need any placeholder there.
You can use the conditional operator ?:
in JavaScript to set the attribute placeholder
either to a value or to an empty string. Setting the placeholder to an empty string should be the same as having it not set for most common use-cases.
To access the correct placeholder value, you'll need to assign index
to a local template variable j
.
<ng-container *ngFor="let text of texts[i]; let j = index">
{{ text }}
<input type=text [placeholder]="condition ? myWords[j] : ''">
</ng-container>
I'm using a similar case with a boolean.
<!-- if extraKilometers is true then I'll type 'Who?' else I'll type 'What?' -->
<textarea class="form-control input-wide-400"
placeholder="{{extraKilometers ? 'Who?' : 'What?'}}"
aria-label="distance"
formControlName="description"
rows=2
id="transportDescription"></textarea>
In your specific example I'd use:
<ng-container *ngFor="let text of texts; let j = index">{{text}}
<input type="text" placeholder="{{ myWords[j] }}" >
</ng-container>
Here's a stackblitz demo with the info you've given using this method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With