在AngularJS中使用<select>和<option>

5
我有一个锚点标签,并根据从对象中获取的日期更改我的视图。我想将其更改为选择选项,但我所做的方式不起作用。 这是锚点标记的语法:
 <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

(注意:保留了 HTML 标签)

尝试在你的选择器上使用ng-change="home.selectedPr = item",并且使用ng-model。 - Vivz
3个回答

7

请查看AngularJS select指令API参考 - 使用ngRepeat生成select选项

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>


更新

我明白了,像这个 示例 一样的内容需要与模型匹配。

请务必使用 ng-model指令

对于非字符串类型的值,请使用 ng-value指令

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


我已经尝试了,但它没有起作用,我不明白为什么: 请参考这个例子 - Hesham El Masry
@HeshamElMasry,我已经在你的plunkr中包含了一个片段。希望这能帮到你。 - Nikhil Mohanan

1

经过不断尝试和搜索,我找到了一个解决方案,所以我必须使用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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,