我正在使用ng-repeat实现一个下拉列表。我想在输入框中添加占位符,因为第一次它是空白的(没有选择下拉选项)。所以我想默认设置占位符。我不想留空白下拉输入框。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedName" ng-options="x for x in names">
</select>
</div>
答案 0 :(得分:1)
添加选项
<select ng-model="selectedName" ng-options="x for x in names">
<option disabled selected value>Please Select</option>
</select>
答案 1 :(得分:0)
您可以添加选项
<option value="" disabled selected>Please Select</option>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["","Emil", "Tobias", "Linus"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedName" ng-options="x for x in names">
<option value="" disabled selected>Please Select</option>
</select>
</div>
答案 2 :(得分:0)
您可以在选择中设置默认值uisng ng-init
。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-init="selectedName = names[0]"
ng-model="selectedName"
ng-options="x for x in names">
</select>
</div>
答案 3 :(得分:0)
你可以这样试试。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedName" ng-options="x for x in names">
<option value="" selected>Please select</option>
</select>
</div>
答案 4 :(得分:0)
此处禁用添加选项:
<option disabled selected value>Select here</option>
代码: https://codepen.io/anon/pen/YYxzxj
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedName" ng-options="x for x in names">
<option disabled selected value>Select here</option>
</select>
</div>