AngularJs - ng-options object.object

时间:2016-12-20 05:26:10

标签: angularjs drop-down-menu angularjs-scope angularjs-ng-repeat angularjs-ng-options

我在angularjs的选择框中显示国家和城市,这些工作正常。当我拨打{{country}}时,国家/地区的ng-model会显示输出。然而,城市的ng模型不能被称为{{city}}及其模态名称。我想将其称为{{city}}而不是{{city.city}}。请注意,除了城市模式之外,两个选择框都正常工作,无法显示{{city}}

否则,当我通过ng-repeat而不是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 个答案:

答案 0 :(得分:1)

选中此link来自可靠的国家/地区状态下拉列表框

更改 HTML代码

<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 :(得分:1)

点击此http://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven示例

Html代码供参考:

+-------+----------+---------------+--------------------------+----------------------------------------------+
|ORDERID|CustomerID|DoughnutOrderID|Date                      |SpecialHandlingInstructions                   |
--------------------------------------------------------------------------------------------------------------
|1      |1         |1              |November, 04 2015 00:00:00|Please Include plates and napkins

JS代码供参考:

 <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="angular.js@1.5.x"></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>