我正在尝试使用 AngularJS 从 HTML View 中检索选定的选项,但我没有找到方法,因为我看到的所有解决方案都考虑了参数“值”,但我在这里没有它,因为我事先不知道选项列表的内容。
这是HTML代码:
<h5>Satellite selection</h5>
<div>
<select id="selectedSatellite">
<option ng-repeat="sat in passes">
{{sat.SAT_NAME}}
</option>
</select>
</div>
提前致谢。
最佳答案
使用以下代码。 ng-options
指令根据提供的数组自动创建 option
元素。 ng-model
检索选择的值
<select ng-model="selectedName" ng-options="sat.SAT_NAME for sat in passes">
</select>
另请参阅以下示例:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
$scope.selectedName = "";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<h3>Selected Value : {{selectedName}}</h3>
<select ng-model="selectedName" ng-options="x for x in names">
</select>
</div>
关于javascript - 使用 ng-repeat 检索选定的选项 angularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49099146/