I am trying to create a dynamic select
component for my application. This component would be used in various forms throughout my application. In some cases, there may be some predefined value that may be provided to the select
, in which case the value would be selected as default by the drop-down. In order to achieve this functionality I have looked over SO and found various questions that answer this problem, but are using NgModule
as a directive for adding default value, whereas I am using formControlName
for my component. I have tried a solution myself (code added below) but it doesn't seem to be working. I am reluctant to use ngModule
with formControlName
because the feature is deprecated in Angular 6 (the version I am using). How do I achieve the functionality?
component.html
<div [formGroup]="group">
<select id="{{ id }}" formControlName="{{ formControlName }}" class="form-control">
<option value="" disabled selected>{{placeholder}}</option>
<option *ngFor="let item of data" [ngValue]="item.id">{{item.name}}</option>
</select>
</div>
component.ts
import { Component, ViewEncapsulation, Input } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-dropdown',
templateUrl: './dropdown.component.html',
styleUrls: ['./dropdown.component.scss'],
encapsulation: ViewEncapsulation.None
})
/**
* This class is used to create a generic dropdown component. Population is done on runtime
*/
export class DropdownComponent{
/**
* Define FormGroup
*/
@Input() group: FormGroup;
/**
* Define formControlName
*/
@Input() formControlName: string;
/**
* Define id
*/
@Input() id: string;
/**
* Define data
*/
@Input() data: any;
/**
* Define placeholder
*/
@Input() placeholder: string;
/**
* For any predefined value being passed
*/
@Input() predefined: string;
constructor() { }
}
what I tried (ts file remained the same)
<div [formGroup]="group">
<select id="{{ id }}" formControlName="{{ formControlName }}" class="form-control">
<ng-template *ngIf="predefined !== undefined; else default">
<option *ngFor="let item of data" [selected]="item.id === predefined">{{item.name}}</option>
</ng-template>
<ng-template #default>
<option value="" disabled selected>{{placeholder}}</option>
<option *ngFor="let item of data" [ngValue]="item.id">{{item.name}}</option>
</ng-template>
</select>
</div>