0

I am displaying countries and cities in a select box in angularjs, which are working fine. ng-model of country displays the output when I call {{country}}. However ng-model of city couldn't be called as {{city}} with its modal name. I want to call it as {{city}} rather than {{city.city}}. Note, both select boxes are working fine except the modal of city, which couldn't be displayed {{city}}

Otherwise, can I obtain city results when I select country through ng-repeat rather than ng-options

<select name="Country" ng-model="country" class="item item-input item-select major" required>
                                    <option value=""> Select a Country  </option>
                                    <option ng-repeat="country in countries" value="{{country.iso_code_3}}">{{country.name}}</option>
                                </select>


    <select name="City" ng-model="city" data-ng-options="city.name for city in cities| filter:{CountryCode: country}:true" class="item item-input item-select major" required>
                                    <option value="">-- Select a City --</option>
                                </select>

2 Answers 2

1

Click this Demo example

Html Code for reference:

 <html ng-app="myapp">
  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link href="style.css" rel="stylesheet" />
    <script data-semver="1.5.8" src="https://code.angularjs.org/1.5.8/angular.js" data-require="[email protected]"></script>
    <script src="app.js"></script>
  </head>
  <body ng-controller="MainCtrl">
    <select ng-model="country" data-ng-options="country for country in countries" ng-change="countryChange()">
      <option value="">Select country</option>
    </select>
    <br>
    <select ng-model="state" ng-options="state for state in allStates">
      <option value="">Select state</option>
    </select>
    <br>
    Country: {{country}}
    <br>
    State: {{state}}
  </body>
</html>

JS code for reference:

var app = angular.module('myapp', []);

app.controller('MainCtrl', function($scope) {
  $scope.countries = ["USA","Canada"];
    $scope.states = [{
        "name": "Alabama",
        "countryId": "USA"
      }, {
        "name": "Alaska",
        "countryId": "USA"
      }, {
        "name": "Arizona",
        "countryId": "USA"
      }, {
        "name": "Alberta",
        "countryId": "Canada"
      }, {
        "name": "British columbia",
        "countryId": "Canada"
    }];

    $scope.countryChange = function(){
      $scope.allStates = [];
      angular.forEach($scope.states, function(value){
        if($scope.country == value.countryId){
          $scope.allStates.push(value.name);
        }
      });
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Check this link fro dependable country state drop-down list box

alter your HTML code below

<html ng-app="app">
  <head>
    <script  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>
  <body ng-controller="CountriesController">
    <select ng-model="country" data-ng-options="country.name for country in countries" ng-change="updateCountry()">
      <option value="">Select country</option>
    </select>
    <br>
    <select ng-model="state" ng-options="state.name for state in availableStates">
      <option value="">Select state</option>
    </select>
    <br>
    Selected Country: {{country}}
    <br>
    Selected State: {{state}}
  </body>
</html>

script.js

var app = angular.module("app", []);

function CountriesController($scope) {
    $scope.countries = [{
        "name": "USA",
        "id": 1
      },{
        "name": "Canada",
        "id": 2
    }];
    $scope.states = [{
        "name": "Alabama",
        "id": 1,
        "countryId": 1
      }, {
        "name": "Alaska",
        "id": 2,
        "countryId": 1
      }, {
        "name": "Arizona",
        "id": 3,
        "countryId": 1
      }, {
        "name": "Alberta",
        "id": 4,
        "countryId": 2
      }, {
        "name": "British columbia",
        "id": 5,
        "countryId": 2
    }];

    $scope.updateCountry = function(){
      $scope.availableStates = [];

      angular.forEach($scope.states, function(value){
        if(value.countryId == $scope.country.id){
          $scope.availableStates.push(value);
        }
      });
    }
}

1 Comment

This code is fine thanks, however you should call {{country.name}} to display country name. I want to just call country in the ng-modal and then I can use $scope.country in my controller. How to do that?

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.