2

I am trying to use the search feature in AngularJS (in Ionic framework). I have a template consisting of different views and a common header for all views. On each view, a different list appears using JSON. I want to use the search feature for each view.

 <form class="form-inline">
      <div class="form-group">
          <input type="text" ng-model="search" class="form-control" placeholder="Search">
      </div>
 </form>

<ion-item class="item-button-right item-avatar item-icon-right item item-thumbnail-left" ng-repeat="text in texts|filter:search" type="item-text-wrap" href="#/otherContent/texts/{{text.id}}">

This code works fine if I use the above search form and ion-item in one single view. But does not work if I add search form in searchbar and put <ion-item> tag in the different page.

I hope this makes sense? Can anyone please suggest how can we implement this. I need searchbar in the header which is common for all views and it should search for each view when it is loaded. Any pointers are truly appreciated.

1 Answer 1

1

If you have a top level controller which is added to like html or body tag (if not then define a top level controller like GlobalController and add it to your <html> or <body> tag so that we can keep the global data there and it's scope can be globally accessible app-wide. Basically we are trying to avoid the $rootScope usage and mimicking the $rootScope usage.), then define an empty object in that global controller:

 $scope.globalModel = {};

Then, change your ng-model like:

<input type="text" ng-model="globalModel.search" class="form-control" placeholder="Search">

And finally modify your search filter like:

<ion-item class="item-button-right item-avatar item-icon-right item item-thumbnail-left" ng-repeat="text in texts|filter:globalModel.search" type="item-text-wrap" href="#/otherContent/texts/{{text.id}}">

The above should work. What we are doing here is that defining an object in a parent scope so that we can prevent the problem of Angular Scopes Inheritance.

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

1 Comment

Thanks alot! Working great! :)

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.