I am using ng2-select in my template for selecting the items, there is a button named "Add" which when click should show another select field with list of other categories of items. For this the simplest version of my code looks like this:
@Component({
moduleId: module.id,
selector: 'client-user-detail',
templateUrl: `<div #one>
<ng-select #selectRole [allowClear]="true"
[multiple]="true" (data)="refreshValue($event)"
(selected)="onSelectRole($event)"
(removed)="removed($event)"
(typed)="typed($event)"
placeholder="Choose/Search">
</ng-select>
<button [disabled]="isDisable"
class="btn btn-default"
(click)="add()">
<i class=" fa fa-save"></i>
Add
</button>
`,
styleUrls: ['clientuserdetail.component.css']
})
export class TestComponent {
@ViewChild('selectRole') public select: SelectComponent;
@ViewChild('one') d1:ElementRef;
constructor(
private renderer: Renderer
) { }
add() {
let htmlText = `<ng-select #selectRole [allowClear]="true"
[multiple]="true" (data)="refreshValue($event)"
(selected)="onSelectRole($event)"
(removed)="removed($event)" (typed)="typed($event)"
placeholder="Choose/Search">
</ng-select>`;
this.renderer.invokeElementMethod
(this.d1.nativeElement,'insertAdjacentHTML',
['beforeend',htmlText]);
}
}
The above code doesn't create a select field. I read somewhere about component Resolver can be useful in such circumstances but I didn't grasp clear way to use it. If there is anyone who can simply explain me about how this problem can be solved, I will be very grateful. I need a clear illustration about how the components can be dynamically added or appended in angular 2. Thank you in advance.