Alex
Alex

Reputation: 5214

Angularjs not letting me specify filter on ng-click

I have a ng-repeat list of items I'd like to filter based on a set of check boxes on a page. I have a search box that I have linked up to work with filtering successfully - however when I want to specify search queries in ng-clicks I am misunderstanding the scope of the variables, or how they are linked I think.

Below is my code I am trying to use to modify the search criteria.

<button ng-click="searchText = a">Click me</button>
<label>Search: <input ng-model="searchText"></label>

A bit further down is my ng-repeat with a filter:

<div ng-repeat="value in present | filter:searchText">

However I am not able to set the searchtext to 'a' once a user clicks the button.

Putting it in the scope seems to change the variable itself but not update the filter, and I'm not sure I understand why this is.

Any ideas?

Upvotes: 0

Views: 33

Answers (2)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

if you need to it to be String a

then

<button ng-click="searchText = 'a'">Click me</button>

defined a as a string 'a',

If we define it as <button ng-click="searchText = a">Click me</button> then a must be a variable bound to scope.

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

The expression within ng-click="searchText = a" is assigning the value of searchText to a scope variable a which would be undefined.

Quote the string value:

ng-click="searchText = 'a'"

Upvotes: 2

Related Questions