javascript 通过 ng-click 将 ng-model 变量传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34270769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing ng-model variable through an ng-click to a function
提问by gurps
I am trying to use a select and options tag with HTML. The data is being brought through using ng-repeat
, when the user chooses an item from the list and presses the get button, I want to pass what the user has chosen from the options list into a function using ng-click
我正在尝试使用带有 HTML 的选择和选项标签。数据正在通过 using ng-repeat
,当用户从列表中选择一个项目并按下获取按钮时,我想将用户从选项列表中选择的内容传递给使用ng-click
HTML
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 Code
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)
}
})
回答by Alexandre
Add ng-model on select and use it in your ng-click :
在选择时添加 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>
回答by mak k
Do something like this:
做这样的事情:
<label class="item item-input">
<textarea placeholder="Comments" ng-model="textModel" ></textarea>
</label>
<a class="button button-positive" ng-lick="closeModal(textModel)">Save</a>