I have found multiple posts including ngSwitchCase in angular 2 instead of ngSwitchWhen, How to bind to '*ngSwitchCase', and Angular 2 - ngSwitchCase. Google wasn't helpful at all.
I am trying to create a simple form for entering search criteria. The first component is a select box and the second component an input box. Depending on the type of search you're doing, I want to use a different input for email, phone number, etc.
I'm feeding my select from constants defined in my form.
const SEARCH_BY_EMAIL = 'email';
const SEARCH_BY_PHONE = 'phone';
const SEARCH_BY_POSTAL_CODE = 'postalCode';
@Component({
selector: 'app-choose-search-criteria',
templateUrl: './choose-search-criteria.component.html',
styleUrls: ['./choose-search-criteria.component.css']
})
export class ChooseSearchCriteriaComponent implements OnInit {
searchTypes = [
{value: SEARCH_BY_EMAIL, label: 'Email'},
{value: SEARCH_BY_PHONE, label: 'Phone Number'},
{value: SEARCH_BY_POSTAL_CODE, label: 'Zip Code'}
];
searchBy: string;
searchValue = '';
constructor(protected dialogRef: MatDialogRef<ChooseSearchCriteriaComponent>,
@Inject(MAT_DIALOG_DATA) public data: ChooseCriteriaDialogData) { }
ngOnInit() {
}
}
<h2>Enter Search Values</h2>
<div class="formFields">
<mat-form-field>
<mat-label>Search By</mat-label>
<mat-select [(value)]="searchBy">
<mat-option *ngFor="let searchType of searchTypes" [value]="searchType.value">
{{searchType.label}}
</mat-option>
</mat-select>
</mat-form-field>
<div [ngSwitch]="searchBy">
<mat-form-field *ngSwitchCase="SEARCH_BY_EMAIL">
<input matInput placeholder="Enter Email Address" type="email" [(ngModel)]="searchValue"/>
</mat-form-field>
<mat-form-field *ngSwitchCase="SEARCH_BY_PHONE">
<input matInput placeholder="Enter Phone Number" type="number" [(ngModel)]="searchValue"/>
</mat-form-field>
<mat-form-field *ngSwitchCase="SEARCH_BY_POSTAL_CODE">
<input matInput placeholder="Enter Zip Code" type="number" [(ngModel)]="searchValue"/>
</mat-form-field>
<p *ngSwitchDefault>Select how you wish to search.</p>
</div>
</div>
Problem is that it doesn't seem to be finding my constants on the ngSwitchCase. I've also tried {SEARCH_BY_POSTAL_CODE} and {{SEARCH_BY_POSTAL_CODE}} but these don't seem to work either.
So, I'm stuck. Advice, please?
Move constants inside of component class as a readonly
fields.
From template you can reference only properties and methods of component's class.