0

I have done this HTML for weather search by inputCity and when it returns an array, I want to give people a choice to choose from a list:

    <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>

My js file:

.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;

and the link in factory:

        getWeather : function(inputCity) {
            return $http({
                url: 'http://api.wunderground.com/api/KEY/conditions/q/' + inputCity + '.json',
                method: 'GET'
            })
        }

When array comes, can I do that on ng-click it chooses a city name (person can use zmw:{{data.zmw}} instead of inputCity or real city name in search field as that info comes with array) and send an AJAX request?

Making it more clear:

I have made a form with input (where should be written city name) and a search button. When Button is clicked weather information appears below. But when person types in a city, which is not the only one with the same name in the world, like for San Francisco (there is 10 of them - in Argentina, US and so on), then weather API returns array of all possible cities instead of weather information. I made it to that point, where if array is returned, it displays all the cities that API returned. What I want to do now is when person clicks on one of the displayed city, ng-click triggers the same AJAX call but with good search input ( zmw:{{data.zmw}} works fine ). Maybe I should do antoher function for that, but then it shows some errors?

2
  • Question is not clear. Explain problem in more detail Commented Mar 29, 2015 at 20:11
  • I wrote a little more details, hope that helps! Commented Mar 29, 2015 at 20:20

1 Answer 1

1

You can get the click event to trigger the AJAX call by changing this line

<tr ng-repeat="data in cityArray" ng-click="inputCity=='zmw:{{data.zmw}}'">

with

<tr ng-repeat="data in cityArray" ng-click="submitForm('zmw:' + data.zmw)">
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, so simple but helped so much!:)

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.