1

I wonder how to connect the search button to the input field.

controllers.js

  angular.module('weatherApp.controllers', [])
 .controller('weatherCtrl', function($scope, $http) {

     $http.jsonp('http://api.openweathermap.org/data/2.5/find?q=' + $scope.SearchCity + '&type=like&APPID=&callback=JSON_CALLBACK')             
         .success(function(data){
             console.log('success');
             $scope.cities = data;

     });

});

all.html

<input type="text" ng-model="SearchCity" placeholder="Search for city" />
<input type="submit" ng-click="Search" Value="Search" />

<tr ng-repeat="city in cities.list | limitTo: 10 | filter:SearchCity">
  <td>{{city.name}}</td>
</tr>

app.js

angular.module('weatherApp', ['weatherApp.controllers', 'ngRoute']).config(['$routeProvider', function($routeProvider){
    $routeProvider.when('/all', {templateUrl: 'templates/all.html', controller:'weatherCtrl'});
    $routeProvider.otherwise({redirectTo: '/all'});
}]);

2 Answers 2

1

Try something like this:

Controller:

.controller('weatherCtrl', function($scope, $http) {

  $scope.cities = [];

  $scope.callOpenWeatherMap = function(searchCity){
    $http.jsonp('http://api.openweathermap.org/data/2.5/find?q=' + searchCity + '&type=like&APPID=&callback=JSON_CALLBACK')
      .then(function(data){
        console.log('success', data);
        $scope.cities = data;
    }).catch(function(error){
        console.log('error', error);

    });
  };

});

HTML:

  <input type="text" ng-model="searchCity" placeholder="Search for city" />
  <input type="submit" ng-click="callOpenWeatherMap(searchCity)" Value="Search" />

  <tr ng-repeat="city in cities.list | limitTo: 10 | filter:searchCity">
    <td>{{city.name}}</td>
  </tr>

UPDATE

To improve your code you could create an OpenWeatherMap service where place all mathods/calls to the OpenWeatherMap api:

Service:

.service('OpenWetherMap', function($http){

  this.find = function(searchCity){
    return $http.jsonp('http://api.openweathermap.org/data/2.5/find?q=' + searchCity + '&type=like&APPID=&callback=JSON_CALLBACK');
  };

})

Controller:

.controller('weatherCtrl', function($scope, OpenWetherMap) {

  $scope.callOpenWeatherMap = function(searchCity){
    OpenWetherMap.find(searchCity).then(function(data){
      console.log('success', data);
      $scope.cities = data;
    }).catch(function(error){
      console.log('error', error);
    });
  };

});
Sign up to request clarification or add additional context in comments.

2 Comments

I get this in mozilla console: success Object { data: Object, status: 200, headers: headersGetter/<(), config: Object, statusText: "load" }. it doesnt list the cities im searching for though
Try changing this line to: console.log('success', data.data);
1

I might be misunderstanding what you're asking, but you should be able to wrap your jsonp call in a $scope.Search function?

4 Comments

I fairly new to coding and not sure what you mean with wrapping. For example, when I write stockholm in the search field I want to it to put the input data into the $scope.searchCity. But I still don't know how ng-click is working
$scope.Search = function () { $http.jsonp(...); }
I've been struggleing with it since I don't know how to connect the button to my input field. That's what im looking for! :)
You are including SearchCity in your jsonp call. That is set with ng-model. Your ng-click should call Search as a function e.g. ng-click="Search()"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.