I want to make a select box component. Simplified components:
@Component({
moduleId: module.id,
selector: 'ng2-select',
template: `
<div class="container">
<div class="placeholder"></div>
<div class="options"
*ngIf="selectService.isSelectorVisible()"
[@animateState]="selectService.getSelectorAnimState()">
<ul>
<ng-content></ng-content>
</ul>
</div>
</div>
`
})
export class SelectComponent {}
@Component({
moduleId: module.id,
selector: 'ng2-option',
template: `
<li (click)="selectValue($event)"
[class.disabled]="disabled"
[class.active]="value == selectService.getSelectedValue()">
<div class="inner" #contentWrapper>
<ng-content></ng-content>
</div>
</li>
`
})
export class OptionComponent {}
I use the component like:
<ng2-select placeholder="Select an option">
<ng2-option value="1">Option 1</ng2-option>
<ng2-option value="2" disabled="true">Option 2</ng2-option>
<ng2-option value="3" selected="true">Option 3</ng2-option>
<ng2-option value="4">Option 4</ng2-option>
<ng2-option value="5">Option 5</ng2-option>
</ng2-select>
What I would like to have in my SelectComponent is the information of which option is selected. Problem is that this information is stored within the children of the component and not added to the dom due to *ngIf.
I know that I can do:
- Pass information of selected option as Input of SelectComponent.
- Use visibility hidden instead of *ngIf and find it out from ElementRef.
but I don't want to do that this way. I would like to keep it as it is. Is there a way I could find out which of the ng2-option has property selected set to true even if it's not added to the DOM?