javascript - How to send data from input to service? -
i have problem sending data form input service. example have 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>
and service geting data 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; }); }]);
how can send city name input after clicking button "go" construct own api link ? , display data in view ? mean http://api.openweathermap.org/data/2.5/forecast/city?q=value_from_the_input&units=metric&mo
you should assign input ng-model
directive, this:
<input type="text" class="form-control" placeholder="city name..." ng-model="city.name">
assign button ng-click
directive, this:
<button class="btn btn-default" type="button" ng-click="getforecast(city)">go!</button>
finally, add getforecast
function controller, this:
$scope.getforecast = function (city) { forecast.getforecast($scope.city).then(function (data) { // response }, function (err) { // error }); }
for work should change service 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'); } }; }]);
Comments
Post a Comment