我有一小段代码可以查询第 3 方 API。是这个吗:
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;
});
};
在 html 方面,现在我只是根据一个特定的搜索在 li 上显示带有 ng-repeat 的 UL。像这样:
<h1>Search</h1>
<ul>
<li ng-repeat="result in results">{{result.title}}</li>
</ul>
我的问题是,我如何创建一个输入字段,将输入的文本与搜索查询绑定(bind)在一起?比如把字符串引入 ?=string:
$http.get('http://api.discogs.com/database/search?type=artist&q=string&page=1&per_page=200')
有什么想法吗?
谢谢! 埃里克
最佳答案
使用 $scope.$watch
为输入变量设置监听器。
<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);
}
工作 JSFiddle
关于javascript - 绑定(bind)输入与 ngResource get() 搜索与 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22366107/