In my application, there is an input field that can be filled either by searching for an item or by clicking on a checkbox. When the user clicks on the checkbox, the input should be automatically filled with the default value valueText
. How can I detect when the checkbox is selected and set the value to [ngModel]
.
public foundedElement: Element[];
public defaultValue: boolean;
public valueText = "text";
constructor(public elementFacade: ElementFacade) {}
addField(value) {
return currentElement.field = value;
}
searchElement(e): void {
const {value} = e.target;
if (value.length > 1) {
this.elementFacade.getAll({name: value}).subscribe(el => this.foundedElement = el);
}
}
<input-container>
<input #elementInput
[mdAutocomplete]="autoElement"
(input)="searchElement($event)"
[ngModel]="currentElement ? currentElement.field : ''"
/>
<md-autocomplete #autoElement="mdAutocomplete">
<md-option
*ngFor="let el of foundedElement"
(onSelectionChange)="addField(el.name)">
{{el.name}}
</md-option>
</md-autocomplete>
</input-container>
<md-checkbox [checked]="defaultValue">Default</md-checkbox>