4

I have a search bar and check box as so:

<input type="text" placeholder="Search..." (keyup)="onKey()" [(ngModel)]="input">
<input type="checkbox" (click)="clearSearch()" [(ngModel)]="checkbox">View All

Whenever anyone types into the search bar I want the checkbox to become unchecked. Likewise, whenever anyone checks the checkbox, I want the search bar to be cleared.

I've tried the following:

export class App {
 checkbox = ['checked']
 clearSearch() { 
 this.input = '';
};
onKey() { 
 checkbox = ['unchecked']
}

But it obviously doesn't quite work.

Here is a plunker with the above code: https://plnkr.co/edit/uAYxswpoz18jlRUqAMn5?p=preview

Which is the best way to achieve this functionality?

Thanks!

0

2 Answers 2

2

You were almost there. First of all, I advice You to use boolean values for checkboxes data models. Array evaluates to true so checkbox is still checked. If you reference instance properties, You should reference them by using word this. So the method onKey() should be :

onKey() { 
 this.checkbox = false
}

plunker

Sign up to request clarification or add additional context in comments.

Comments

1

In your function make this.checkbox to be false.

 <input type="checkbox" (click)="clearSearch()" [(ngModel)]="checkbox">View All

your function

 onKey() { 
     this.checkbox = false;
  }

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.