【发布时间】:2025-12-23 07:55:11
【问题描述】:
我是 angular js 的初学者,在我的应用程序中,我使用了一个 web api,它提供了我们输入 pincode 时的位置。我在文本字段的 keyup 事件上调用我的控制器中的一个函数,但它会引发一些错误,并且有时不会使 get 调用正确。有没有办法解决这个问题?,我真正需要的是,只有当用户停止或等待在文本字段中输入下一个键时才进行 api 调用
HTML
<input type="text" ng-keyup="check();" ng-model="place" placeholder="Enter a pincode"/>
<p>{{data.city}}</p>
Controller.js
app.controller('MyController', function($scope,$http) {
$scope.check = function(){
var req = {
method: 'GET',
url: 'https://sphirelabs-indian-pin-codes.p.mashape.com/pincode.php?code='+$scope.place,
headers: {
"Key": "api key",
"Accept": "application/json"
},
}
$http(req).
success(function(data, status, headers, config) {
$scope.data = data;
}).
error(function(data, status, headers, config) {
conosole.log("Error");
})
};
});
【问题讨论】:
标签: javascript angularjs get timeout angular-http