我正在尝试使用 AngularJS 创建实时搜索功能。我有一个输入字段:
<input type="text" placeholder="Search" data-ng-model="title" class="search">
它可以在范围内传递搜索关键字,以便我可以执行实时搜索(JS)并将结果直接显示到 DOM
var app = angular.module("DB", []);
app.controller("Controller", function($scope, $http) {
$scope.details = [],
$http.defaults.headers.common["Accept"] = "application/json";
$http.get('http://api.org/search?query=<need to pass search name here>&api_key=').
success(function(data, status, headers, config) {
}).
error(function(data, status, headers, config) {
//handle errors
});
});
最佳答案
在 Angular Controller 内部使用监视表达式。
$scope.$watch('title', function (newValue, oldValue) {
if(newValue != oldValue) {
$http.get('http://api.org/search?query=' + newValue + '&api_key=')
.success(function(data, status, headers, config) { /* Your Code */ })
.error(function(data, status, headers, config) { /* Your Code */ });
}
});
关于javascript - 将 ng-data 传递到作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25711642/