通过ng-click将ng-model变量传递给函数

时间:2015-12-14 15:30:01

标签: javascript angularjs

我正在尝试使用带有HTML的select和options标记。数据通过ng-repeat使用,当用户从列表中选择项目并按下获取按钮时,我想将用户从选项列表中选择的内容传递到使用ng-click的函数中

HTML

<ul>
 <li class="col-xs-4"><a href="#favourites">Favourites</a></li>
</ul>
<div ng-controller="favouritesController">
 <select class="col-xs-12" name="weather">
  <option ng-repeat="weatherList in weatherLists" ng-model="city">{{weatherList.place}}</option>
 </select>
 <div class="col-xs-12">
  <button type="button" name="button" class="deleFav" ng-click="getFavWeather(city)">Get</button>
 </div>
</div>

Javascript代码

var myApp = angular.module('myApp', ['ngRoute'])

 myApp.controller('favouritesController', function($scope, $http, $rootScope, $route, $location) {
  $scope.getFavWeather = function(weatherList){
   console.log("yes yes yes")
   console.log($scope.city)
  }
 })

2 个答案:

答案 0 :(得分:9)

在选择中添加ng-model并在ng-click中使用它:

<div ng-controller="favouritesController">
 <select class="col-xs-12" name="weather" ng-model="citySelected">
  <option ng-repeat="weatherList in weatherLists" ng-model="city">{{weatherList.place}}</option>
 </select>
 <div class="col-xs-12">
  <button type="button" name="button" class="deleFav" ng-click="getFavWeather(citySelected)">Get</button>
 </div>
</div>

答案 1 :(得分:-1)

做这样的事情:

SELECT COUNT(*) AS admission_total
FROM yourTable
GROUP BY MONTH(admission_date)