javascript - Autocomplete textbox options from JSON -
this answer close i'm trying do. i'm branching original plunker new plunker
the original plunker has text box auto-completes auto-complete options hard coded in list.
i've added service new plunker gets information stackoverflow badges in json. how can have auto-complete use json data provide list of badge names auto-complete options?
<html lang="en"> <head> <meta charset="utf-8" /> <title>jquery ui autocomplete - default functionality</title> <script data-require="angular.js@1.3.9" data-semver="1.3.9" src="https://code.angularjs.org/1.3.9/angular.js"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" /> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <script> var app=angular.module('app',[]); app.factory('badges', ['$http', function($http) { return $http.get('https://api.stackexchange.com/2.2/badges?page=1&order=desc&sort=rank&site=stackoverflow') .success(function(data) { return data; }) .error(function(err) { return err; }); }]); app.controller('ctrl', ['badges', function($scope, badges){ badge_names = how make list of badge names badges.success?; $scope.availabletags = badge_names; }); $scope.complete=function(){ $( "#tags" ).autocomplete({ source: $scope.availabletags }); } }); </script> </head> <body ng-app="app" ng-controller="ctrl"> <div class="ui-widget"> <label for="tags">tags: </label> <input id="tags" ng-keyup="complete()"/> </div> </body> </html>
here go, checkout plunk: http://plnkr.co/edit/igpyrcdfqt1pn53piqsc. quite simple , should understandable. there might better optimized ways of achieving this, i've done. i'm pasting code here well.
javascript
<script> var app = angular.module('app', []); app.factory('badges', ['$http', function($http) { return $http.get('https://api.stackexchange.com/2.2/badges?page=1&order=desc&sort=rank&site=stackoverflow') .success(function(data) { return data; }) .error(function(err) { return err; }); }]); app.controller('ctrl', ['$scope', 'badges', function($scope, badges){ badgenames = []; badges.then(function(response){ for(var i=0; < response.data.items.length; i++){ badgenames[i] = response.data.items[i].name; } }); $scope.availabletags = badgenames; $scope.complete = function () { console.log('running'); $( "#tags" ).autocomplete({ source: $scope.availabletags }); }; }]);
html
<body ng-app="app" ng-controller="ctrl"> <div class="ui-widget"> <label for="tags">tags: </label> <input id="tags" ng-keyup="complete()"/> </div> </body>
you'll see in code above traverse through json make array consisting of names of badges. ideally, should done via api using filters wasn't familiar stack overflow api why, did way.
Comments
Post a Comment