【发布时间】:2015-03-29 19:51:53
【问题描述】:
我已经通过inputCity 完成了这个用于天气搜索的 HTML,当它返回一个数组时,我想让人们从一个列表中进行选择:
<form class="form-container" ng-submit="submitForm(inputCity)">
<input class="input-field" type="text" ng-model="inputCity" placeholder="City">
<input class="button-style" type="submit" value="Show Weather">
</form>
<table class="cities-table" ng-if="isArray">
<tr ng-repeat="data in cityArray" ng-click="inputCity=='zmw:{{data.zmw}}'">
<td>{{data.city}} in {{data.country_name}}</td>
</tr>
</table>
我的js文件:
.controller('WeatherController', [
'$scope',
'Weather',
function($scope, Weather) {
/* TODO: implement */
$scope.weatherData = [];
$scope.submitForm = function(inputCity){
Weather.getWeather(inputCity).success(function(data){
$scope.isArray = angular.isArray(data.response.results);
$scope.cityArray = data.response.results;
以及工厂中的链接:
getWeather : function(inputCity) {
return $http({
url: 'http://api.wunderground.com/api/KEY/conditions/q/' + inputCity + '.json',
method: 'GET'
})
}
当数组出现时,我可以在ng-click 上执行此操作吗?它会选择一个城市名称(人们可以在搜索字段中使用 zmw:{{data.zmw}} 而不是 inputCity 或真实城市名称,因为该信息与数组一起提供)并发送 AJAX 请求?
说得更清楚:
我已经制作了一个带有输入(应该在哪里写城市名称)和一个搜索按钮的表单。单击按钮时,天气信息将显示在下方。但是当人们输入一个城市时,这不是世界上唯一一个同名的城市,比如旧金山(其中有 10 个 - 在阿根廷、美国等),然后天气 API 返回所有可能的数组城市而不是天气信息。我做到了这一点,如果返回数组,它会显示 API 返回的所有城市。我现在想要做的是当人们点击其中一个显示的城市时,ng-click 会触发相同的 AJAX 调用,但具有良好的搜索输入( zmw:{{data.zmw}} 工作正常)。也许我应该为此做另一个功能,但是它显示了一些错误?
【问题讨论】:
-
问题不清楚。更详细地解释问题
-
我写的详细一点,希望对你有帮助!
标签: javascript angularjs