在 AngularJS Angular-Formly select 中移除空选项,可以使用以下代码:
angular.module('myApp', ['formly'])
.run(function(formlyConfig) {
formlyConfig.setType({
name: 'select',
template: '',
wrapper: ['bootstrapLabel', 'bootstrapHasError'],
defaultOptions: {
ngModelAttrs: {
'': {
value: "options.defaultValue || null"
},
// This was added to avoid errors with a disabled field and ng-required
required: {
expression: 'fc.$viewValue === undefined || fc.$viewValue !== ""'
},
},
templateOptions: {
placeholder: '',
options: [],
}
},
controller: ['$scope', function($scope) {
$scope.to.options = $scope.to.options || [];
$scope.to.options.unshift({label: 'Select an option', value: null});
}]
});
});
这段代码定义了一个名为“select”的表单类型,并在控制器中添加了选项数组。项数组中包含一个标签为“Select an option”,值为 null 的选项,它就是空选项。您可以根据自己的需求更改此选项。