1

我正在创建一个 Web 应用程序,在其中我使用下拉列表和 angularjs 来更新我的数据库,

这是我的下拉列表的语法

<select ng-model="ucomname" ng-change="uucomname(o)">
       <option ng-repeat="o in comnamelistfun" value="{{o.comname}}">{{o.comname}}</option>
</select>

此下拉列表中的第一个值变为空白,但我希望下拉列表的第一个值成为下拉列表的第一个值,当我使用 ng-init 时,我无法在 ng-model 中发送值任何想法?

4

3 回答 3

1

你可以使用这个。 http://plnkr.co/edit/1EVs7R20pCffewrG0EmI?p=preview

    (function() {
  'use strict';

  angular
    .module('app', [])
    .controller('HomeCtrl', HomeCtrl);

  HomeCtrl.$inject = ['$scope'];

  function HomeCtrl($scope) {

    $scope.cities = [
      { id: 1, name: 'London' },
      { id: 2, name: 'Chicago' },
      { id: 3, name: 'Moscow' },
      { id: 4, name: 'Mumbai' },
      { id: 5, name: 'Casablanca' }
      ];

    // Pre-select city by id
    $scope.selectedCityId = 1;

    // Pre-select city by object
    $scope.selectedCity = { id: 1, name: 'Casablanca' };

  }
})();

    <!DOCTYPE html>
<html ng-app="app">

<head>
  <link rel="stylesheet" href="style.css" />
</head>

<body ng-controller="HomeCtrl">
  <h3>Data List</h3>

  <table>
    <tbody>
      <tr ng-repeat="city in cities">
        <td>{{city.id}}</td>
        <td>{{city.name}}</td>
      </tr>
    </tbody>
  </table>

  <h3>Selected City (by Id)</h3>
  selectedCityId: <strong>{{selectedCityId}}</strong>
  <br/>
  <br/>
  <label>Select City
    <select ng-model="selectedCityId" ng-options="city.id as city.name for city in cities">
      <option value="">-- Select City --</option>
    </select>
  </label>

  <hr/>

  <h3>Selected City (by object)</h3>
  selectedCity: <strong>{{selectedCity}}</strong>
  <br/>
  <br/>
  <label>Select City
    <select ng-model="selectedCity" ng-options="city as city.name for city in cities track by city.id">
      <option value="">-- Select City --</option>
    </select>
  </label>

  <hr/>

  <!-- Scripts -->
  <script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
  <script src="HomeCtrl.js"></script>

</body>
</html>
于 2016-10-19T06:22:55.213 回答
1

首先在你的控制器中,

app.controller('cardController', ['$scope',
    function($scope) {
    $scope.ucomname = $scope.comnamelistfun[0]
}])

然后选择框是,

<select ng-model="ucomname" ng-change="uucomname(o)" ng-options="o for o in comnamelistfun">
</select>

这对你有用。

于 2016-10-19T06:17:47.967 回答
0

试试这个代码。

<select ng-model="ucomname" ng-options= "o.comname for o in comnamelistfun track by o.comname">
</select>

并在您的控制器中。放这个。

$scope.ucomname = $scope.comnamelistfun[0]; //This will select the first element.

我希望它应该工作:)

于 2016-10-19T06:16:41.057 回答