angularjs - Use a component multiple times in a parent component -
i have issues using component multiple times in parent component. each control filled set selected value. when values changed passed parent control correctly.
select controls contain same options.
problem is, when changing value of last select element, other 2 selects change same value. selectedvalue of other 2 selects still holds correct value.
hope images show effect.
page loaded initial values: after changing first 2 selects ok: when change third select, error happens:
the child component, displays select control. select control filled according given @input() property.
@component({ selector: 'app-evaluation', templateurl: `./evaluation.component.html?v=${new date().gettime()}`, styleurls: ['./evaluation.component.css'] }) export class evaluationcomponent implements oninit, onchanges { @input() private grade: gradedetail = null; @input() private selectedvalue: number = null; @input() private disabled: boolean = false; @output() private onselect = new eventemitter<number>(); private gradevalues: number[] = null; private id: string; constructor(elm: elementref) { this.id = elm.nativeelement.getattribute('id'); } ngoninit() { } ngonchanges() { this.gradevalues = []; if (this.grade == null) return; (let = this.grade.minvalue; <= this.grade.maxvalue; = + this.grade.discretization) { this.gradevalues.push(i); } } selected() { this.onselect.emit(this.selectedvalue); } }
the html looks this:
<select [id]="id" [attr.name]="id" [(ngmodel)]="selectedvalue" [disabled]="disabled" class="form-control" (change)="selected()"> <option *ngfor="let value of gradevalues" [ngvalue]="value" [selected]="grade?.value == value">{{value}}</option> </select> {{selectedvalue}}
this component used 3 times in parent component:
<app-evaluation id="val1" [grade]="item" [selectedvalue]="item?.val1" (onselect)="setval1($event)"></app-evaluation> <app-evaluation id="val2" [grade]="item" [selectedvalue]="item?.val2" (onselect)="setval2($event)"></app-evaluation> <app-evaluation id="val3" [grade]="item" [selectedvalue]="item?.value" (onselect)="setval3($event)"></app-evaluation>
after hours of investigating , debugging found solution. reason name of property i've bound 'selectedvalue' of 3rd usage of component. mustn't named 'value'. renamed , components work expected.
Comments
Post a Comment