【问题标题】:AngularJS Help: Connecting a Search Input into an REST InterfaceAngularJS 帮助:将搜索输入连接到 REST 接口
【发布时间】:2015-08-16 12:09:46
【问题描述】:

过去几天我一直在学习 AngularJS,但是我被困在一个部分。我想要做的是允许用户输入搜索(当然是在输入小部件中),我希望搜索连接到在线 API(OpenWeatherAPI)并从在线提取 JSON 数据。然后我希望结果显示在网页中。

我已经完成了使用 AngularJS 提取 JSON 数据的源代码。我只是坚持将搜索查询连接到 API。这里是提取 REST API 的源代码:

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

11 // Note: Controller To Access Weather Data From OpenWeatherAPI (Content Type: JSON)  
12 app.controller('FetchController', function ($scope, $http) { 
13     $http.get("http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139", request) 
14     .then(function (response) { 
15         if (response.status == 200) { 
16             // Note: Important To Print Your Response To Anaylse The JSON Recieved 
17             // Note: For Example OpenWeatherAPI Add Additional Param So Have it 
18             // Note: Like <code>$scope.info = response.data;</code> To Anaysle It 
19             $scope.info = response.data; 
20         } 
21     }); 
22 

23     // Note: Request Object For Extra Types 
24     var request = { 
25         headers: 'application/json', 
26         method: 'GET' 
27     }; 
28 }); 

【问题讨论】:

  • 您的问题到底是什么?没有数据显示?错误信息?
  • 基本上将搜索的输入连接到REST API。所以假设我正在寻找日本。我将其输入到搜索框中,然后单击提交。然后日本这个词被添加到 API 的 URL 中,然后我收到了信息。我坚持的基本上是当我点击“提交”按钮时,将其连接到 URL。

标签: javascript json angularjs rest


【解决方案1】:

这是您访问数据的方式: 一个工作的fiddle

HTML:

<div ng-controller="MyCtrl">
   The weather from {{weatherLocation}}
    <p>The wind speed is: {{windSpeed}} and the degree is {{windDegree}}</p>
</div>

控制器:

angular.module('myApp',[]).controller("MyCtrl", MyCtrl);


function MyCtrl($scope, $http) {
    $http.get("http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139").success(function(data){
    $scope.weatherLocation = data.name;
    $scope.windSpeed = data.wind.speed;
    $scope.windDegree = data.wind.deg;

        console.log(data)
    }) 
}

编辑

看完你的cmets,看我更新的fiddle

<div ng-controller="MyCtrl">
   The weather from {{weatherLocation}}
    <p>The wind speed is: {{windSpeed}} and the degree is {{windDegree}}</p>
    <input type="text" ng-model="location"/>
    <button ng-click="findWeather()">submit</button>
</div>

angular.module('myApp',[]).controller("MyCtrl", MyCtrl);


function MyCtrl($scope, $http) {

    $scope.location = "";

    $scope.findWeather = function(){
    http://api.openweathermap.org/data/2.5/weather?q=washington
            $http.get(" http://api.openweathermap.org/data/2.5/weather?q=" + $scope.location).success(function(data){
    $scope.weatherLocation = data.name;
    $scope.windSpeed = data.wind.speed;
    $scope.windDegree = data.wind.deg;

    }) 
    }
}

【讨论】:

  • 我可以做那个部分,我主要寻找的是将我提交的内容连接到我坚持的 URL。基本上,当我按下提交按钮时,它会将我输入的任何内容放入 URL
  • 您能否举例说明您希望 url 的外观以及您从文本框中获得的数据?因为我在 url 中没有看到位置名称
  • 好的,网址如下:
  • 谢谢,我很抱歉没有在最后一条评论中输入正确的 URL,因为我的电脑在我输入一半的时候就死机了。感谢您的帮助
  • 如果这个答案对您有用,请投票并接受答案,谢谢
猜你喜欢
  • 2016-02-28
  • 2014-07-03
  • 2015-10-20
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多