如何从输入的数据发送到服务? [英] How to send data from input to service?

查看:169
本文介绍了如何从输入的数据发送到服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有发送数据的形式输入到该服务的一个问题。
比如我有一个输入:

I have a problem with a sending data form input to the service. For example I have an input:

<input type="text" class="form-control" placeholder="City name...">
<span class="input-group-btn">
      <button class="btn btn-default" type="button">Go!</button>
</span>

和这对REST API歌厅数据的服务:

And a service which is geting data for rest api:

app.factory('forecast', ['$http', function($http) { 
  return $http.get('http://api.openweathermap.org/data/2.5/forecast/city?q=Warsaw&units=metric&mo') 
            .success(function(data) { 
              return data; 
            }) 
            .error(function(err) { 
              return err; 
            }); 
}]);

我怎样才能从输入点击按钮后发送城市名称走出去建立我自己的API链接?然后显示在查看这些数据?
我的意思是这样的http://api.openweathermap.org/data/2.5/forecast/city?q=VALUE_FROM_THE_INPUT&units=metric&mo

推荐答案

您应该指定您输入的 NG-模型指令,如:

You should assign your input an ng-model directive, like this:

<input type="text" class="form-control" placeholder="City name..." ng-model="city.name">

您分配一个按钮 NG-点击指令,如:

<button class="btn btn-default" type="button" ng-click="getForecast(city)">Go!</button>

最后,添加的getForecast 功能控制器,是这样的:

Finally, add getForecast function to your controller, like this:

$scope.getForecast = function (city) {
    forecast.getForecast($scope.city).then(function (data) {
        // do something with the response
    }, function (err) {
        // do something about the error
    });
}

对于这个工作,你应该改变你的服务是这样的:

For this to work you should change your service to something like this:

app.factory('forecast', ['$http', function($http) { 
   return { 
       getForcast: function (city) {
           $http.get('http://api.openweathermap.org/data/2.5/forecast/city?q=' + city.name + '&units=metric&mo'); 
       }
   };
}]);

这篇关于如何从输入的数据发送到服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆