I am trying to create a generic drop down control in Angular 2, using Semantic UI. I have the following code:
import {ElementRef, Component, OnInit, EventEmitter} from "angular2/core";
import {DropdownValue} from "./dropdown-value";
declare var jQuery: any;
@Component({
selector: 'my-dropdown',
inputs: ['selectedItem', 'items', 'label'],
outputs: ['selectedItemChange'],
template: `
<div class="field">
<label>{{label}}</label>
<select class="ui selection dropdown" [ngModel]="selectedItem.value" (ngModelChange)="onChange($event)">
<!--<option value="" selected>Please Select</option>-->
<option *ngFor="#item of items" [value]="item.value">{{item.label}}</option>
</select>
</div>`
})
export class MyDropdownComponent implements OnInit {
items: DropdownValue[];
selectedItem: DropdownValue;
selectedItemChange: EventEmitter<any> = new EventEmitter();
constructor(private elementRef: ElementRef) {
}
ngOnInit(): any {
this.items.unshift(new DropdownValue('', 'Please Select'));
this.selectedItem = this.selectedItem || this.items[0];
//jQuery(this.elementRef.nativeElement).find('select').dropdown();
}
onChange(newValue) {
let selectedItem = this.items.find(item => item.value == newValue);
this.selectedItemChange.emit(selectedItem);
}
}
This actually works just fine as it is (without Semantic UI JS styling), the issue is, as soon as I uncomment the line //jQuery(this.elementRef.nativeElement).find('select').dropdown(); the 'Please Select' is no longer visible and it does not show the initial selection.