gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:22:31 25 4
gpt4 key购买 nike

我试图在 HTML 中使用选择和选项标签。数据是通过使用 ng-repeat 带来的,当用户从列表中选择一个项目并按下 get 按钮时,我想将用户从选项列表中选择的内容传递到一个函数中使用ng-点击

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

最佳答案

在选择上添加 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>

关于javascript - 通过 ng-click 将 ng-model 变量传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34270769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com