Search code examples
htmlangularjsdrop-down-menung-options

Empty fields in drop-down list


Here is the .js file which load the right json data, from an API

   (function() {

      angular.module('application', [])
        .factory('Forecast', ['$http', '$q', function($http, $q) {
            var ApiAddr = "api.com/";
          var forecast = {};

          forecast.getResults = function(timeStart, timeEnd) {
            // We map application varaible names with API param names
            var httpParams = {
              type: "global",
              time: "minute",
              tsmin: timeStart,
              tsmax: timeEnd
            };
            return $http.get(ApiAddr, {
              params: httpParams,
              cache: true
            }).then(function(result) {
              return result.data;
            });
          };

          return forecast;

        }])
        .controller('SampleCtrl', ['$scope', 'Forecast', function($scope, Forecast) {
          $scope.forecastReport = '';

          $scope.getForecast = function() {

            var t1 = Date.parse($scope.timeStart);
            var t2 = Date.parse($scope.timeEnd);

            Forecast.getResults(t1, t2)
              .then(function(report) {
                $scope.result = report;
              }).catch(function(err) {
                $scope.result = '';
                console.error('Unable to fetch forecast report: ' + err);
              });
          };
        }]);

    })();

And there is the HTML file which display the result

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
</head>
<body ng-app="application" ng-controller='SampleCtrl'>
    <div ng-controller="SampleCtrl">
        <label>Time Start</label>
            <input type="datetime-local" ng-model='timeStart'></input>
        <label>Time End</label>
            <input type="datetime-local" ng-model='timeEnd'></input>
        <button ng-click="getForecast()">Get Forecast</button>
        <label>Départements</label>
        <select ng-model='selecteditem' ng-options="result.zipcode for dept in result">
            <option value="">-- Choisir département --</option>
        </select>

        <div>
            <b>Forecast result: </b>
        </div>
        <pre>{{selecteditem.count}}</pre>

    </div>
</body>
</html>

The problem is the following. I enter the two dates, and i obtain the right json.

After, when the json data is uploaded, i have the drop-down list, and when i click on a element of the drop down list (the zipcode of the department), i have the number of calls displayed.

But there is a problem : in the drop down menu, i have always nothning written, even if it is matching with elements from the JSON.

What is the reason of this comportment and how to solve this ?

What i have : The empty spaces in the drop down list

The result json output


Solution

  • The error were in ng-options. The right code is :

    <select ng-model='selecteditem' ng-options="dept.zipcode for dept in result">