i have an array of components, i want to show the existing components in that array but it doesnt work, i suspect that a new component is created and showed in the ngfor and not the one in the array because i do see new components get created but with a default value and not the current value that get passed to them, what am i doing wrong?
html:
<div #visualTextDiv class = "visual_text" [ngStyle]=setStyles()>
<div *ngFor="let lineCounter of visualDivArray">
<app-visual-text-div></app-visual-text-div>
</div>
</div>
typescript:
if(this.visualDivArray.length < currentDivIndex+1){
let newv = new VisualTextDivComponent()
this.visualDivArray.push(newv);
console.log("creating new " + this.visualDivArray.length);
}
this.visualDivArray[currentDivIndex].setData(textValue);
Have your VisualTextDivComponent
create an array of objects. Pass that array to the ngFor
. Then pass each item of the array into your app-visual-text-div
component like this:
<div #visualTextDiv class = "visual_text" [ngStyle]=setStyles()>
<div *ngFor="let lineCounter of visualDivArray">
<app-visual-text-div [curLineCounter]="lineCounter"></app-visual-text-div>
</div>
</div>
curLineCounter
or some better name, is a variable in the app-visual-text-div
component.
AppVisualTextDiv.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-visual-text-div',
templateUrl: './AppVisualTextDiv.html'
})
export class AppVisualTextDiv{
@Input() curLineCounter: {
'id': number,
'someValue': string,
};
}
AppVisualTextDiv.html
<div id="{{curLineCounter.id}}">
<span>{{curLineCounter.someValue}}</span>
</div>
<!-- or whatever your use case provides -->
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