在angular中使用select来传递多个参数

Use select in angular to pass multiple arguments

本文关键字:参数 select angular      更新时间:2023-09-26

在Angular Js中,是否有一种方法可以像使用链接列表一样使用select元素?

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

<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>
控制器

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

你是这个意思吗?如果您希望

使用控制器值而不是作用域值

<select ng-options="['a,b','c,d']" ng-model="selected" ng-change="ctrl.change(selected)">