I am writing an application which UI wise is almost exactly like Google. I arrive at landing page I have a search box which on submit directs you to results page. Here you have the same search box and other directives in which you can switch between modes: eg. web, image. Currently I have:
on landing page: form with action="results.html" which passes the parameters in the url.
<form name="search" role="form" action="results.html">
<div class="input-group input-group-search">
<input type="text" class="form-control" ng-model="blab" name="q" required>
<span class="input-group-addon">
<button class="btn-search" ng-disabled="search.$invalid">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
</span>
<input type="hidden" name="mode" value="web"/>
</div>
</form>
on results I am just using ng-submit="search()" and ng-model on the input. The search box is within the searchController.
What is the right method for doing this as a custom directive, with the following behaviour:
- On landing page on submit direct to results page
- On results page make a search without reloading the page and change the location to the right parameters?
I run something similar on my site currently. I however did not wrap my search into a directive because it is it's own page.
For how I have it setup, I have a search page
site.com/search
(which would be your landing page for example) That page has its own controller/viewSearchController
. On the same page lies a separate container which can essentially list items that are inside an array. Finally, the entire page has anApplicationController
.Now, the
SearchController
andApplicationController
are obviously separate and therefore cannot access each-other's properties or methods. What they can do however, is either share a factory/service or broadcast information. For the simplicity of this example we will have them share a service calledSearchService
.If you still wish to use a directive, you can easily turn the
SearchController
into a directive and utilize the same concept for yourself.Basic Plunkr Example Here
SearchService
The
SearchService
will contain useful properties and methods for searching, but all you need right now is just a simpleArray
to contain a list of search results.ApplicationController
The
ApplicationController
scope will have a reference toSearchService
so that you can useng-repeat
and list out the actual contents of the search results.SearchController
The
SearchController
scope will also have a reference toSearchService
so that it can modify theSearchService.arrSearchResults
array, thus altering what will be displayed on the page. It will also contain methods to interact with the form.It will also alter the URL location when a search is executed.
The Page
Tabs
For switching the type of search result (web, image, etc) you can create a variable within the
SearchService
that controls the state of the search, and thus what type of search to run.SearchService.typeOfSearch = "web";
This sets the state toweb
and thus can be interacted within the page and app.You can then have various
ng-repeat
throughout the page all showing results for different states:I have updated the Plunkr to demonstrate.