Skip to content

Instantly share code, notes, and snippets.

@suras
Last active January 3, 2016 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suras/8424198 to your computer and use it in GitHub Desktop.
Save suras/8424198 to your computer and use it in GitHub Desktop.
angular.js auto suggest
'use strict';
barterApp.directive('autosuggest', function($timeout, $http) {
return {
restrict: "E",
scope: {
modelupdate:"=",
suggestions:"=",
urlsend:"@"
},
template: '<ul><li ng-repeat="suggest in suggestions" ng-click="updateModel(suggest)">{{suggest}}</li></ul>',
link: function (scope, element) {
scope.$watch('modelupdate', function() {
$timeout(function(){
$http.post(scope.urlsend).then(function(data){
scope.suggestions = data.data;
console.log(data.data);
});
}, 3000);
});
scope.updateModel = function(value){
scope.modelupdate = value;
}
}
};
});
usage
<div ng-controller = "controller">
<input ng-model = "books">
<autosuggest modelupdate = "books" suggestions = "book_suggestions" urlsend="/book_suggestions.json"> </autosuggest>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment