{"id":7603,"date":"2012-09-16T20:51:34","date_gmt":"2012-09-16T15:21:34","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7603"},"modified":"2017-08-03T14:08:20","modified_gmt":"2017-08-03T08:38:20","slug":"angularjs-text-suggestions","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/","title":{"rendered":"AngularJS : Text Suggestions"},"content":{"rendered":"<p>Let&#8217;s implement text suggestions <a title=\"angular.js development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">functionality using AngularJS<\/a>. It provides suggestions while we type into the text field. For example, if we have a list of fruits and the user types &#8216;ap&#8217;, it will show all fruit names that contain these letters.<\/p>\n<p>[html]<br \/>\n&amp;lt;!Doctype html&amp;gt;<br \/>\n&amp;lt;html ng-app&amp;gt;<br \/>\n&amp;lt;head&amp;gt;<br \/>\n&amp;lt;script type=&amp;quot;text\/javascript&amp;quot; src=&amp;quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.0.1\/angular.min.js&amp;quot;&amp;gt;&amp;lt;\/script&amp;gt;<br \/>\n&amp;lt;script type=&amp;quot;text\/javascript&amp;quot; src=&amp;quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.8.1\/jquery.min.js&amp;quot;&amp;gt;&amp;lt;\/script&amp;gt;<br \/>\n&amp;lt;script type=&amp;quot;text\/javascript&amp;quot;&amp;gt;<br \/>\n\tfunction FruitsController($scope){<br \/>\n\t\t$scope.fruits=[&#8216;apple&#8217;,&#8217;mango&#8217;,&#8217;banana&#8217;,&#8217;strawberry&#8217;,&#8217;pineapple&#8217;,&#8217;pina colada&#8217;,&#8217;raspberry&#8217;,&#8217;cherry&#8217;,&#8217;peach&#8217;,&#8217;pear&#8217;,&#8217;apricot&#8217;];<br \/>\n\t\t$scope.toggleGuess=function(){<br \/>\n\t\t\tif($scope.query==&#8221;) $(&#8216;p&#8217;).hide();<br \/>\n\t\t\telse $(&#8216;p&#8217;).show();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n&amp;lt;\/script&amp;gt;<br \/>\n&amp;lt;style type=&amp;quot;text\/css&amp;quot;&amp;gt;<br \/>\n\tp{background-color: #ff0; width:10%; padding:5px; margin:1px; display:none;}<br \/>\n&amp;lt;\/style&amp;gt;<br \/>\n&amp;lt;\/head&amp;gt;<br \/>\n&amp;lt;body&amp;gt;<br \/>\n&amp;lt;div ng-controller=&amp;quot;FruitsController&amp;quot;&amp;gt;<br \/>\n\tStart typing the Fruit name to get possible guesses :&amp;lt;br \/&amp;gt;<br \/>\n\t&amp;lt;input type=&amp;quot;text&amp;quot; ng-model=&amp;quot;query&amp;quot; ng-change=&amp;quot;toggleGuess();&amp;quot;&amp;gt;<br \/>\n\t&amp;lt;p ng-repeat=&amp;quot;fruit in fruits | filter:query&amp;quot;&amp;gt;{{fruit}}&amp;lt;\/p&amp;gt;<\/p>\n<p>&amp;lt;\/div&amp;gt;<br \/>\n&amp;lt;\/body&amp;gt;<br \/>\n&amp;lt;\/html&amp;gt;<br \/>\n[\/html]<\/p>\n<p>I have also included and used jquery library in this code. Inside FruitsController, we initialized a list of fruits, which is used to show fruit names on the page using ng-repeat in line 23. Initially, we hide this list using css, so that user can see suggestions only after they start typing into the text box.<\/p>\n<p>The text box is bound to a variable &#8216;query&#8217;, which means whatever user types in the textbox, is contained in the query variable.<\/p>\n<p>[html]&amp;lt;p ng-repeat=&amp;quot;fruit in fruits&amp;gt;[\/html]<\/p>\n<p>will display a list of all fruits. We are passing its output to filter function. The filter function uses the query value to create a new array that contains only those records that match the query.<\/p>\n<p>Whenever user types something into the textbox, the change event is fired and we bind it to a function toggleGuess using ng-change directive of AngularJS. toggleGuess function uses jquery to check if user has typed something into the textbox and if it is so, it displays the list of fruits that matches the query. If textbox goes empty, it hides the list of fruits.<\/p>\n<p>Have Fun \ud83d\ude42<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS : A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS : Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS : Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS : Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS : Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\"> AngularJS : Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS : Multiple Views, Layout Template and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS : Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s implement text suggestions functionality using AngularJS. It provides suggestions while we type into the text field. For example, if we have a list of fruits and the user types &#8216;ap&#8217;, it will show all fruit names that contain these letters. [html] &amp;lt;!Doctype html&amp;gt; &amp;lt;html ng-app&amp;gt; &amp;lt;head&amp;gt; &amp;lt;script type=&amp;quot;text\/javascript&amp;quot; src=&amp;quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.0.1\/angular.min.js&amp;quot;&amp;gt;&amp;lt;\/script&amp;gt; &amp;lt;script type=&amp;quot;text\/javascript&amp;quot; src=&amp;quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.8.1\/jquery.min.js&amp;quot;&amp;gt;&amp;lt;\/script&amp;gt; &amp;lt;script type=&amp;quot;text\/javascript&amp;quot;&amp;gt; [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[1439,3429],"tags":[3955,955,4697,55,972,973],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7603"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7603"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7603\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}