Question

I've a little piece of code that makes a query into a 3rd party API. Is this one:

angular.module('myApp', ['ngResource'])
function Hello($scope, $http) {

$http.get('http://api.discogs.com/database/search?type=artist&q=alva-noto&page=1&per_page=200').
    success(function(data3) {
        $scope.results = data3.results;
    });

};

In the html side, right now I just display a UL with ng-repeat on the li, based in one particular search. Like so:

<h1>Search</h1>
  <ul>
    <li ng-repeat="result in results">{{result.title}}</li>
  </ul>  

My question is, how could I create an input field that binds the text written inside with the search query? Like introducing the string into ?=string:

$http.get('http://api.discogs.com/database/search?type=artist&q=string&page=1&per_page=200')

Any thoughts?

Thanks! Eric

Was it helpful?

Solution

Use $scope.$watch to set a listener to the input variable.

<div class="test" ng-controller="Ctrl">
    <input type="text" ng-model="name">
  <ul>
    <li ng-repeat="result in results">{{result.title}}</li>
  </ul> 
<div>

JS

var app = angular.module('app', []);

function Ctrl($scope, $http) {
    var get_results = function(name) {
            $http.get('http://api.discogs.com/database/search?type=artist&q='+ name + '&page=1&per_page=200').
        success(function(data3) {
            $scope.results = data3.results;
        });
    }
    $scope.name = ''
    $scope.$watch('name', get_results, true);

}

Working JSFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top