我有汽车对象,我将它绑定在我的选择框上。我想让我的selectbox的所有值重复选项。我试图通过函数传递参数来获取我选择的汽车的值。我该怎么做?
我的代码如下。
谢谢!
var app = angular.module("App", []);
app.controller("Controller", function($scope) {
$scope.val = "";
$scope.cars = [{
"no": "1",
"name": "Audi"
}, {
"no": "2",
"name": "Opel"
}, {
"no": "3",
"name": "Volkwagen"
}, {
"no": "4",
"name": "Skoda"
}, {
"no": "5",
"name": "Mercedes"
}];
$scope.Do = function(resp) {
$scope.val = "current car's val is: " + resp;
};
});
<!DOCTYPE html>
<html ng-app="App">
<head>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<meta charset="utf-8">
<title>AngularJS</title>
</head>
<body ng-controller="Controller">
<select ng-model="araba" ng-options="item.name for item in cars" ng-change="Do(item.no)"></select>
<p>{{val}}</p>
</body>
</html>
答案 0 :(得分:1)
许多人在这里提出相同的问题,我们可以通过以下三种方式从下拉菜单中获取一个选定的值(主要是)
请尝试这种方式
<select ng-model="araba" ng-options="item.name for item in cars" ng-change="Do(this)"></select>
js side
$scope.Do = function(resp) {
$scope.val = "current car's val is: " + resp.no;
};
<select ng-model="araba" ng-options="item.name for item in cars" ng-change="Do(item)"></select>
js side
$scope.Do = function(resp) {
$scope.val = "current car's val is: " + resp.no;
};
<select ng-model="araba" ng-options="item.name for item in cars" ng-change="Do()"></select>
js side
$scope.Do = function() {
$scope.val = "current car's val is: " + $scope.araba.no;
};
答案 1 :(得分:0)
更改选择,
<select ng-model="araba" ng-options="item.name for item in cars" ng-change="Do()"></select>
将Do()
函数更改为
$scope.Do = function() {
$scope.val = "current car's val is: " + $scope.araba.no;
};
您不需要将所选值传递给do()
函数。相反,您可以使用scope
变量$scope.araba
来获取所选值。
这是Plunker