【问题标题】:Using <select> and <option> in AngularJS在 AngularJS 中使用 <select> 和 <option>
【发布时间】:2017-12-14 02:50:09
【问题描述】:

我有这个锚标记,我会根据来自对象的日期更改我的视图。我正在尝试将其更改为一个选择选项,但它没有按照我的方式工作。

这是锚标记语法:

 <a href="#" ng-repeat="item in home.prData" ng-click="home.selectedPr = item; $event.preventDefault();
 $event.stopPropagation();">{{item.date}}</a>

当我使用选择选项时,我正在尝试将其更改为那样

 <select st-search="{{item.date}}" class="show-tick form-control dropdown_custom_list">
      <option value="">- select a date -</option>
      <option ng-repeat="item in home.prData" value="{{home.selectedPr = item}}">{{item.date}}
      </option>
  </select>

我为我想要实现的目标创建了一个示例 plunkr:

mycode

【问题讨论】:

标签: javascript angularjs dropdown ng-options angularjs-select


【解决方案1】:

AngularJS select Directive API Reference - Using ngRepeat to generate select options

angular.module('ngrepeatSelect', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.data = {
     model: null,
     availableOptions: [
       {id: '1', name: 'Option A'},
       {id: '2', name: 'Option B'},
       {id: '3', name: 'Option C'}
     ]
    };
 }]);
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="ngrepeatSelect">
  <div ng-controller="ExampleController">
  <form name="myForm">
    <label for="repeatSelect"> Repeat select: </label>
    <select name="repeatSelect" id="repeatSelect" ng-model="data.model">
      <option value="">- select an option -</option>
      <option ng-repeat="option in data.availableOptions" 
              value="{{option.id}}">{{option.name}}
      </option>
    </select>
  </form>
  <hr>
  <tt>model = {{data.model}}</tt><br/>
</div>

更新

我确实明白这一点,所以我作为this example 要做的就是匹配模型

请务必使用ng-model directive

对于字符串以外的值,使用ng-value directive

<select ng-model="mainCtrl.selectedHotel">
    <option value="">-select hotel-</option>
    <option ng-repeat="hotel in mainCtrl.hotels" ng-value="hotel">
        {{hotel.name}}
    </option>
</select>

如需了解更多信息,

【讨论】:

    【解决方案2】:

    在我不断尝试和搜索之后,我找到了一个解决方案,因此我不得不使用 ng-model 并将其分配给我的 selectedPr 作为我的示例。它奏效了

    <select class="form-control selectpicker show-tick" 
            title="Select period" selectpicker ng-model="home.selectedPr"
            ng-options="item.date for item in home.prData">
    </select>
    

    【讨论】:

      【解决方案3】:
      <select st-search="{{item.date}}" ng-model="selectedDate" ng-change="home.selectedPr = selectedDate"
       class="show-tick form-control dropdown_custom_list">
        <option value="">- select a date -</option>
        <option ng-repeat="item in home.prData" value="{{home.selectedPr = item}}">{{item.date}}
        </option>
      </select>
      

      尝试添加一个ng-model 变量并在ng-change 上分配选定的日期。没有ng-modelng-change 将无法工作

      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
      
      <div ng-app="demoApp" ng-controller="mainController">
      
        <select class="show-tick form-control dropdown_custom_list" ng-model="selectedHotel" ng-options="hotel.name for hotel in hotels">
          <option value="">select one--</option>
        </select>
      
        <div>
          Hotel name: {{selectedHotel.name}} Category: {{selectedHotel.category | json}}
        </div>
      </div>
      
      <script>
        // Code goes here
      
        angular.module('demoApp', [])
          .controller('mainController', MainController);
      
        function MainController($scope) {
      
          $scope.selectedHotel = {};
      
          $scope.hotels = [{
            "id ": 1,
            "name": "some hotel 1 ",
            "category": [{
              "id ": 1,
              "hotel_id ": 1,
              "name ": "Cat name 1 ",
              "bnb ": "yes ",
              "simple ": "yes "
            }]
          }, {
            "id ": 2,
            "name": "some hotel 2 ",
            "category": [{
              "id ": 1,
              "hotel_id ": 2,
              "name ": "Cat name 1 ",
              "bnb ": "yes ",
              "simple ": "yes "
            }]
          }];
          // to make first  one default selected..
          //$scope.selectedHotel = $scope.hotels[0];
        }
      </script>

      【讨论】:

      • 我做了,但它不起作用我不明白为什么:refer to this example
      • @HeshamElMasry,我在您的 plunkr 中包含了一个 sn-p w r。希望这会对你有所帮助..
      猜你喜欢
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2016-12-08
      相关资源
      最近更新 更多