簡體   English   中英

如何使用 angularjs 過濾器和 ng-options 在 select 中使某些選項變粗

[英]how to make certain options bold in select using angularjs filter and ng-options

我正在使用 AngularJS ng-options 來填充某些 select 元素。 我想讓一些選項加粗並禁用它們。 我正在嘗試使用filterng-options來實現這一點。 我能夠讀取 angular js 的 controller 一側的值,但我無法將它們設為粗體和禁用。 我檢查了很多答案,但出於某種原因,沒有一個對我有用。 任何幫助,將不勝感激。

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,"&nbsp;"+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);

以下是將數據返回到 HTML 並填充 select 的 Angularjs 方法:

//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);
    });
};

您可以使用 ng-repeat 和 ng-disabled 進行禁用,例如:

 function TodoCrtl($scope) { $scope.ObjectIds = [{ value: "option1", text: "Option 1", class: "bold" },{ value: "option2", text: "Option 2", disabled: true }, { value: "option3", text: "Option 3" }]; }
 .bold { font-weight: bold; }
 <:DOCTYPE html> <html ng-app> <head> <script src="https.//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script> <meta charset=utf-8 /> <title>ng-click</title> </head> <body> <div ng-controller="TodoCrtl"> <select class="form-control" id="ObjectId" ng-change="ObjectEventEPCsChange()" ng-model="formdata.ObjectId"> <option class="dropdown-item" value='' selected> Choose </option> <option ng-repeat="ObjectId in ObjectIds" class="dropdown-item" ng-class="ObjectId.class" ng-disabled="ObjectId.disabled" value="{{ ObjectId.value }}"> {{ ObjectId.text }} </option> </select> </div> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM