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

时间:2017-07-10 11:29:03

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

我有这个锚标记,我根据来自对象的日期更改我的视图。我正在尝试将其更改为选择选项,但它不能像我这样做。 这是锚标记语法:  &lt; a href =“#”ng-repeat =“home.prData中的项目”ng-click =“home.selectedPr = item; $ event.preventDefault();  $ event.stopPropagation();“&GT; {{item.date}}&LT; / A&GT; 当我使用select选项时,我试图改变它  &lt; select st-search =“{{item.date}}”class =“show-tick form-control dropdown_custom_list”&gt;       &lt; option value =“”&gt; - 选择日期 - &lt; / option&gt;       &lt; option ng-repeat =“home.prData中的项目”value =“{{home.selectedPr = item}}”&gt; {{item.date}}       &LT; /选项&GT;   &LT; /选择&GT; 我创建了一个示例plunkr到我想要实现的目标: mycode的

3 个答案:

答案 0 :(得分:4)

请参阅AngularJS select Directive API Reference - Using ngRepeat to generate select options

&#13;
&#13;
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'}
     ]
    };
 }]);
&#13;
<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>
&#13;
&#13;
&#13;

更新

  

我明白这一点,所以我必须做的就是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>

欲了解更多信息,

答案 1 :(得分:2)

<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-change无法在ng-model

下工作

<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>

答案 2 :(得分: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>