Use directivengModel to bind input text element to property named
word, which record the value of user input.
Use directivengChange to monitor the changes of user input. If user input
changes, inputChange() function calls suggestService to provide
suggested words.
suggestService generates random strings to provide suggested words. You
should implement your own here.
'use strict';angular.module('inputSuggestApp',[]).controller('suggestController',['$scope','suggestService',function($scope,ss){$scope.word="";$scope.suggestedWords=[];$scope.inputChange=function(){// empty $scope.suggestedWordswhile($scope.suggestedWords.length>0){$scope.suggestedWords.pop();}$scope.suggestedWords=ss.GetSuggestedWords($scope.word);};$scope.isShowSuggestMenu=function(){return$scope.suggestedWords.length>0;};varinputElm=angular.element(document.querySelector('input'));$scope.suggestMenuStyle={left:inputElm.prop('offsetLeft')+'px',minWidth:inputElm.prop('offsetWidth')+'px',};}]).factory('suggestService',[function(){// implement your suggest function here.// return random strings for demo purpose.functiongetRandomInt(min,max){returnMath.floor(Math.random()*(max-min+1))+min;}functionRandomString(strlen){constchars="abcdefghijklmnopqrstuvwxyz0123456789";varresult="";for(vari=0;i<strlen;i++){result+=chars[getRandomInt(0,35)];}returnresult;}functionGetSuggestedWords(word){// Remove leading and trailing whitespacesvarwordTrimed=word.replace(/(^\s+)|(\s+$)/g,"");varsuggestedWords=[];if(wordTrimed.length>0){for(vari=0;i<10;i++){suggestedWords.push(RandomString(wordTrimed.length));}}returnsuggestedWords;}return{GetSuggestedWords:GetSuggestedWords};}]);