将动态值显示为下拉列表中的第一个元素

时间:2018-06-05 02:41:34

标签: javascript html angularjs

我正在使用angularjs forEach loop。我正在显示带有动态值的下拉列表。 我动态地获得$scope.showCurrentProgram,我希望将其显示为加载页面时列表中选择的第一个元素,并从下拉列表中删除相同的元素($ scope.programs)。

示例js代码:

app.controller("BaseController", function($scope) { 
  $scope.title = "Angular.ForEach"; 
  $scope.showCurrentProgram="Oracle";//dynamically showCurrentProgram value is updated which need to be shown on the dropdown list as the selected item and remove that element from the list
     $scope.programs = ["ALL","C","C++","java","SAP","Oracle",".net","ABAP"];
});

html代码:

  <select name="selectedPrograms" ng-model="selectedProgram" ng-options="x for x in programs" ng-change="updatePrograms()"></select>

预期产量: 在页面加载时将Oracle显示为列表中的第一个元素。 $ scope.showCurrentProgram在运行时动态加载一些值,需要在下拉列表中显示为所选元素(第一个元素)。

1 个答案:

答案 0 :(得分:-1)

只需将 $scope.selectedProgram 设置为您要绑定的值,并将其设置为模板中的ng-model。

<强>演示

var app = angular.module('BaseApp',[]);
app.controller("BaseController", function($scope) { 
  $scope.title = "Angular.ForEach"; 
  $scope.selectedProgram="Oracle"; 
     $scope.programs = ["ALL","C","C++","java","SAP","Oracle",".net","ABAP"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="BaseApp" ng-controller="BaseController">
<select name="selectedPrograms" ng-model="selectedProgram" ng-options="x for x in programs" ng-change="updatePrograms()"></select>
</body>