Search code examples
angularjsng-options

Why the below piece of code is not working for me in angularjs


The below code is to populate a drop down select option with json data provided. BUt it is not populating.

this is not populating a dropdrop with json data in the fucntion

  <select ng-model="selectedName" ng-options="x for x in values.names">
</select>
<select ng-model="places" ng-options="x for x in values.places"></select>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.values ={
    names :["Emil", "Tobias", "Linus"]
    places :["hyd","tnd","sec"]
    }

});
</script>


<p>This example shows how to fill a dropdown list using the ng-options directive.</p>

</body>
</html>

Solution

  • Your JSON should be corrected as,

    {
      "names": [
        "Emil",
        "Tobias",
        "Linus"
      ],
      "places": [
        "hyd",
        "tnd",
        "sec"
      ]
    };
    

    DEMO

    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.values= {
      "names": [
        "Emil",
        "Tobias",
        "Linus"
      ],
      "places": [
        "hyd",
        "tnd",
        "sec"
      ]
    };
    
    });
     
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="myCtrl">
    <select ng-model="selectedName" ng-options="x for x in values.names">
    </select>
    <select ng-model="places" ng-options="x for x in values.places"></select>
    
    </div>