gpt4 book ai didi

javascript - 使用 Angular 中的 select 传递多个参数

转载 作者:行者123 更新时间:2023-12-03 08:37:47 25 4
gpt4 key购买 nike

在 Angular Js 中,有没有一种方法可以像使用链接列表一样使用选择元素?

例如,如果我有一些像这样的链接:

<a ng-click="ctrl.change('argument1','argument2')">One</a>
<a ng-click="ctrl.change('argument3','argument4')">Two</a>
...

有没有办法可以用选择元素做同样的事情?

<select>
<option value="argument1,argument2">One</option>
<option value="argument3,argument4">Two</option>
...
</select>

最佳答案

您可以通过 ng-change 传递变量和模型,希望下面的代码对您有帮助,

模板

<div ng-controller="myCtrl">
<select ng-model="item" ng-options="i.name for i in items" ng-change="changed('hello','world',item)">
<option value="">choose items</option>
</select>
</div>

Controller

function myCtrl($scope) {

$scope.items = [{

"name": "first",
"type" : "type1"

}, {

"name": "second",
"type" : "type2"

}, {

"name": "third",
"type" : "type3"

}];


$scope.changed = function (hello,world,item) {
alert(hello); // argument1
alert(world); //argument2
alert(item.type); //model argument based on the selection
}

}

关于javascript - 使用 Angular 中的 select 传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33144085/

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