【发布时间】:2020-10-19 06:21:45
【问题描述】:
我正在使用 AngularJS ng-options 来填充某些选择元素。我想让一些选项加粗并禁用它们。我正在尝试使用filter 和ng-options 来实现这一点。我能够读取角度 js 控制器端的值,但我无法将它们设为粗体和禁用。我检查了很多答案,但出于某种原因,没有一个对我有用。任何帮助将不胜感激。
HTML Code for the select:
//HTML SELECT
<select class="form-control" id="ObjectId" ng-change="ObjectEventEPCsChange()" ng-model="formdata.ObjectId" ng-options='ObjectId.value as ObjectId.text for ObjectId in ObjectIds |filter:MyFilter'>
<option class="dropdown-item" value='' selected> Choose </option>
<option class="dropdown-item" value="{{ ObjectId.value }}"> {{ ObjectId.text }} </option>
</select>
ÀngularJS Controller function for filter:
//根据条件使一些选项加粗并禁用
$scope.MyFilter = function(item) {
if(item.text == 'ABCD')
{
console.log(item.toString().replace(item.text,"<strong>"+item.text+"</strong>"));
return item.toString().replace(item.text,"<b>"+item.text+"</b>")
}
else
{
return item.toString().replace(item.text," "+item.text);
}
}
Populating of the options:
我在 nodejs 中有选项,以下是填充到 Select 中的选项类型:
var ObjectTypeOptions = [
{value:'Option-1', text:'Option-1'},
{value:'Option-2', text:'Option-2'},
{value:'Option-3', text:'Option-3'},
{value:'ABCD', text:'ABCD'},
{value:'Option-4', text:'Option-4'},
];
var returnData = {'ObjectTypeOptions': ObjectTypeOptions};
callback(returnData);
以下是 Angularjs 方法,它将数据返回到 HTML 并填充选择:
//On load of the page populate the drop-downs
$scope.init = function () {
$http({
url: "/populateFields",
method: "GET"
}).success(function(response) {
$scope.ObjectIds = response.ObjectTypeOptions;
}).error(function(error) {
console.log(error);
});
};
【问题讨论】:
-
请发布您操作的数据示例。或者更好的是,用 Plunker 或类似工具编写一些演示
-
完成,请检查
-
你使用任何引导主题吗?据我所知,本机浏览器下拉菜单没有粗体样式选项
-
是的,我正在使用 Bootstrap 4。
标签: javascript html angularjs angularjs-filter angularjs-controller