代码之家  ›  专栏  ›  技术社区  ›  Nicoleta Wilskon

如何设置所选角度的默认值

  •  0
  • Nicoleta Wilskon  · 技术社区  · 7 年前

    HTML

       <select  ng-init="model.country = cities[currentLocationIdx]"  
    chosen  ng-model="model.country"  
       ng-options=" cities.name for cities in cities" required> 
        <option value="">Select a Location</option> 
        </select>
    

    $scope.model.country = "Bangalore";
        $scope.currentLocationIdx = $scope.cities.findIndex( city => city.name ===  $scope.model.country );
    

    [
      {
        "name": "South Andaman"
      },
      {
        "name": "Nicobar"
      },
      {
        "name": "Adilabad"
      },
      {
        "name": "Anantapur"
      },
      {
        "name": "Chittoor"
      },
      {
        "name": "East Godavari"
      },
      {
        "name": "Bangalore"
      }
    ]
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Abdul Rafay    7 年前

    $scope.model = {};
    

    WORKING DEMO

        2
  •  1
  •   Rakesh Burbure    7 年前

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body ng-app="app" ng-controller="ctrl">
    
        <select name="mySelect" id="mySelect" chosen
                ng-options="city.name for city in cities track by city.name"
                ng-model="country"></select>
    
        {{country}}
    
        <script src="../lib/angular.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-chosen-localytics/1.8.0/angular-chosen.js"></script>
        <script>
            var app = angular.module('app', []);
            app.controller('ctrl', function ($scope) {
    
                $scope.cities = [
                      {
                          "name": "South Andaman"
                      },
                      {
                          "name": "Nicobar"
                      },
                      {
                          "name": "Adilabad"
                      },
                      {
                          "name": "Anantapur"
                      },
                      {
                          "name": "Chittoor"
                      },
                      {
                          "name": "East Godavari"
                      },
                      {
                          "name": "Bangalore"
                      }
                ];
                $scope.country = { "name": "Bangalore" };
            });
        </script>
    
    </body>
    </html>
    
    推荐文章