这是我的代码。当组合框的值更改时,我需要找到一种方法来搜索 $scope.options 中数组中的值。
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html ng-app>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="../angular.js"></script>
<script>
function reportController($scope){
$scope.options=[{name:'option 1', id:'1'},{name:'option 2', id:'2'},{name:'option 3', id:'3'}];
$scope.selectedValue = {name:'option 1', id:'1'};
$scope.updatedName = $scope.selectedValue.name;
$scope.updatedId = $scope.selectedValue.id;
$scope.updateCombo = function()
{
console.log("inside updateCombo");
comboVal = document.getElementById("nameCombo").value;
arrIndex = $scope.options.indexOf(comboVal);
console.log("combo box selected value = " + comboVal + " :: index = "+ arrIndex);
//$scope.selectedValue=$scope.options[index];
}
}
</script>
</head>
<body>
<div ng-controller="reportController">
<select id ="nameCombo" ng-model="selectedValue" ng-change="updateCombo()">
<option ng-repeat="value in options" >{{value.name}}</option>
</select>
<p>Name : {{selectedValue.name}}</p>
<p>ID : {{selectedValue.id}}</p>
</div>
</body>
</html>
我尝试从函数调用中传递索引,但这确实有效。我也尝试过 indexOf,但这不起作用,因为我只有名称,没有组合框中的 id。
最佳答案
你可能想做这样的事情:
<select ng-model="selectedValue" ng-options="opt.name for opt in options"></select>
所选值是“ng-model”并会自动更新。
检查这个Fiddle我为您制作的示例。
关于javascript - 在 Javascript 和 AngularJS 中查找数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23624334/