[英]show dynamic value as the first element in the dropdown list
I am working on angularjs forEach loop.I am showing dropdown list with dynamic values. 我正在开发angularjs forEach循环,正在显示具有动态值的下拉列表。 I am getting $scope.showCurrentProgram
dynamically which i wanted to show as the first element selected in the list when the page is loaded and remove the same element from the dropdown list($scope.programs). 我正在动态获取$scope.showCurrentProgram
,我想在加载页面时显示为列表中选择的第一个元素,并从下拉列表($ scope.programs)中删除相同的元素。
Sample js code: 样本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 code: html代码:
<select name="selectedPrograms" ng-model="selectedProgram" ng-options="x for x in programs" ng-change="updatePrograms()"></select>
expected output: Display Oracle as the first element in the list on page load. 预期的输出:将Oracle显示为页面加载列表中的第一个元素。 $scope.showCurrentProgram loads some value dynamically at runtime which need to be shown as the selected element(first element) in the dropdown list. $ scope.showCurrentProgram在运行时动态加载一些值,这些值需要在下拉列表中显示为所选元素(第一个元素)。
Just set the $scope.selectedProgram
to the value you want to bind and set it as the ng-model in the template. 只需将$scope.selectedProgram
设置为要绑定的值,并将其设置为模板中的ng-model。
Demo 演示版
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.