# Search box directive

You use the `sfSearchBox` to enable the visitors to search your website. The directive provides interface for user input used by the searching functionality.

The `sfSearchBox` is a directive with an isolated scope that is defined in a module with the same name *sfSearchBox*. You can use the directive as an attribute or as an element.\
For more information, see [Isolating the Scope of a Directive](https://docs.angularjs.org/guide/directive#isolating-the-scope-of-a-directive).

## Attributes

The search box directive exposes the following attributes:

 <table>
<colgroup><col><col></colgroup>
<tbody>
<tr>
<td>

<strong>Attribute</strong>

</td>
<td>

<strong>Description
</strong>

</td>
</tr>
<tr>
<td>

<code>sf-action</code><br>

</td>
<td>

This attribute accepts a delegate to a function with one argument defined in the current scope. The search box invokes this delegate when the typed text is to be applied.

</td>
</tr>
<tr>
<td>

<code>sf-min-text-length</code><br>

</td>
<td>

This attribute is an optional parameter. It is an integer specifying what is the minimum length of user input, so that the <code>sfSearchBox</code> directive calls <code>sfAction</code>. For example, if you set this attribute to `-1`, then the directive invokes <code>sfAction</code> on any change in the search box, independently of the length of the input.

</td>
</tr>
<tr>
<td>

<code>sf-placeholder</code><br>

</td>
<td>

This attribute is an optional parameter. It enables you to specify an [HTML \<input> placeholder Attribute](http://www.w3schools.com/tags/att_input_placeholder.asp) for the input without changing the template.

</td>
</tr>
<tr>
<td>

<code>sf-enable-suggestions</code><br>

</td>
<td>

This attribute activates the suggestions functionality. To use the functionality, you must combine the <code>sf-enable-suggestions</code> attribute with the <code>sfGetSuggestions</code> attribute below.

</td>
</tr>
<tr>
<td>

<code>sf-get-suggestions</code><br>

</td>
<td>

When you use the <code>sfEnableSuggestions</code> attribute, the <code>sfGetSuggestions</code> attribute accepts a delegate to a function with one argument defined in the current scope. This function must always return an array of strings that present the appropriate suggestions for the query provided in the arguments.

</td>
</tr>
<tr>
<td>

<code>sf-clear-search-string</code><br>

</td>
<td>

This attribute clears the text typed in search box. The value of the attribute must be a property in your scope.

</td>
</tr>
</tbody>
</table>

## Add a search box directive

The following example demonstrates how to add a search box directive in a widget designer's view.

To enable AngularJS to link the `sfSearchBox` directive in your custom designer view, you must load the script of the directive and add a dependency to the module.\
Perform the following:

1. Sitefinity CMS automatically registers the scripts you need and, if no other designer view with explicitly set priority exists, Sitefinity CMS sets your designer view priority 1. In case you need to have full control over the scripts that are loaded or you want to set custom priority, you can alternatively create your own `DesignerView.YourView.json` file. If you have a `JSON` file that matches the convention (even if empty), this automatic scripts registration will not occur. In the `DesignerView.YourView.json` file, add a scripts array. As a result, the file content should be similar to the following:
   ```JSON
   {
     "priority": 1,
     "components" : ["sf-search-box"]
   }
   ```
2. In the `designerview-yourview.js` file, right before the definition of your custom view controller, place the following code snippet:
   ```JavaScript
   (function () {

       //add the following snippet before the definition of the custom controller:
       var designerModule = angular.module('designer');
       angular.module('designer').requires.push('sfFields');
       angular.module('designer').requires.push('sfSelectors');

   })();
   ```
3. In your `DesignerView.YourView.cshtml` file, place the following tag where you want to render the `sfSearchBox` directive:

   ```HTML
   <sf-search-box sf-placeholder="Narrow by typing" sf-action="applyQuery(query)" sf-enable-suggestions sf-get-suggestions="suggest(query)" 
                  sf-clear-search-string="clearSearch"> 
                  </sf-search-box>
   ```

   The following example passes a delegate to the search box to let the directive choose when the query needs to be applied, based on the attributes. In addition, you enable the suggestions functionality and define a *suggest* function. The function calls a service that gets items based on the current query. The resulting promise is returned after extracting the titles of the items.
4. In your designer's controller, add the following code:
   ```JavaScript
   scope.applyQuery = function (query) {
     scope.query = query;
   };

   scope.suggest = function (query) {
     return itemService.getItemsContaining(query).then(function (data) {
     return data.map(function (item) { return item.title });
     });
   };
   ```
