如何在单击<option>时模拟ng-click或触发某些内容?

时间:2015-10-08 21:44:36

标签: ionic-framework ionic

我有这个选择列表,当用户选择doSomething()时,我需要<option>,我尝试使用ng-click,但它不起作用。
无论如何我可以接近ng-click,但是在<option>标签上?

<select ng-model="MedsRefund.cityLabel">
  <option value="{{city.name}}" ng-repeat="city in MedsRefund.cities'">{{city.name}}
  </option>
</select>

2 个答案:

答案 0 :(得分:1)

使用ng-change确定该项目已更改并采取特定操作

的index.html

<!DOCTYPE html>
<html ng-app="jsbin">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="mainController">
  <pre>{{MedsRefund | json}}</pre>
    <select ng-model="MedsRefund.cityLabel" ng-change="selectedItem()">
      <option value="{{city.name}}" ng-repeat="city in MedsRefund.cities">
        {{city.name}}
      </option>
    </select>
</body>
</html>

app.js

var app = angular.module('jsbin', []);


var mainController = function ($scope) {
  $scope.MedsRefund = {
    cities :[{name:"aName"},{name:"bName"}],
    cityLabel :""
  };

  $scope.selectedItem = function() {
    alert("selected Item " +  $scope.MedsRefund.cityLabel);
  };
};

app.controller('mainController',mainController);

答案 1 :(得分:0)

感谢用户Aaron Saunders我有点破解了我的问题,这是我的解决方案:

  <!-- CITIES POPOVER -->
    <select ng-class="MedsRefund.cityClicked ? 'used-dropdown' : 'selects-dropdown'"
            ng-change="cityClick(city.name)"
            ng-model="city.name">
      <option value="" ng-hide="!MedsRefund.cityClicked" selected>Ciudad</option>
      <option value="{{city.name}}" class="list cities-list"
              ng-repeat="city in MedsRefund.cities | orderBy: 'name' | unique: 'name'">{{city.name}}
      </option>
    </select>

并在控制器中:

$scope.MedsRefund.cityLabel = "Ciudad";