如何在 ng-option 中为每个选项应用字体系列

How to apply font-family in ng-option for each option

我想显示一个组合框,其所有选项字体都将不同。

在 AngluarJS 中使用 ng-options 指令填写 <select> 标签的选项时,我不知道如何为每个选项设置字体系列。

$scope.reportFontload=["Andale Mono","Arial","Arial Black","Bitstream Charter","Century Schoolbook L","Comic Sans MS","Courier 10 Pitch","Courier New","DejaVu Sans"];

<select ng-options="font for font in reportFontload" ng-model="selected">
 <option value="" disabled selected>Font Family</option>        
 </select>

对于 ng-repeat,如果我曾经申请 style="font-family:{{font}}"。它适用于 ng-repeat.

中的每个选项

示例如下:fiddle

<select ng-model="selected">
  <option value="" disabled selected>Fonts</option> 
  <option ng-repeat="font in reportFontload" value={{font}} style="font-family:{{font}}">{{font}}</option>
</select>

我需要知道,为什么 font-family 在 ng-option 中不起作用。指导我任何可能的答案。

您不能创建一个 select(组合框),其选项将具有不同的字体。据我所知,这在 HTML 和 CSS.

中是不允许的

但是,如果您使用不同的组合,例如 Bootstrap 或其他一些组合,则可以实现此目的。

这是一个 bootstrap 示例,其中包含普通的 HTML。

<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
    Dropdown
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
    <li style="font-family:Arial"><a href="#">Action</a></li>
    <li style="font-family:Arial Blackl"><a href="#">Another action</a></li>
    <li style="font-family:Andale Mono"><a href="#">Something else here</a></li>
    <li style="font-family:Courier New"><a href="#">Separated link</a></li>
  </ul>
</div>

Plunker Here

现在要在 angular 中使用,您可以使用 https://angular-ui.github.io/bootstrap/ 或者您可以为其编写自己的实现。

我创建了一个 optionsAttribute 指令,它可以在 ngOptions 呈现选项标签后转换它们的属性。

DEMO

指令

.directive('optionsAttribute', function($parse, $timeout) {
  return {
    restrict: 'A',
    compile: function(tElem, tAttr) {
      var transform = $parse(tAttr.optionsAttributeTransform);
      return function(scope, elem, attr) {
        scope.$watch(attr.optionsAttribute, function(data) {

          var options = elem.children();
          options = Array.prototype.slice.call(options, options.length - data.length);

          angular.forEach(options, function(option, index) {
            var optionElement = angular.element(option);
            var newAttribute;
            var label;

            if(angular.isDefined(attr.optionsAttributeTransform)) {
              newAttribute = transform(scope, { 
                $data: data[index],
                $label: option.innerHTML,
                $index: index
              });

              angular.forEach(newAttribute, function(value, attribute) {
                optionElement.attr(attribute, value);
              });
            }

          });

        });
      };
    }
  };
});

用法

  1. optionsAttribute指令中提供您在ngOptions指令中提供的数组值。

例如

<select ng-options="font for font in reportFontload"
        options-attribute="reportFontload">
</select>
  1. optionsAttributeTransform 指令中添加一个回调 returns 一个 json 对象,表示要附加在每个 option 标签中的属性。回调本身提供了 2 个值:

$data - 代表#1 中提供的数组中的每一项。

$index - 表示 $data.

的索引

例如

HTML

<select ng-options="font for font in reportFontload"
        ng-model="selected"
        options-attribute="reportFontload"
        options-attribute-transform="{ 'style': 'font-family: ' + $data }">
</select>

或者另一种方法是提供函数回调

JAVASCRIPT

$scope.transform = function(fontFamily) {
  return { 'style': 'font-family ' + fontFamily };
};

HTML

<select ng-options="font for font in reportFontload"
        ng-model="selected"
        options-attribute="reportFontload"
        options-attribute-transform="transform($data)">
</select>

限制

该指令仅限于数组,不包括对象,虽然有一种方法可以调整当前指令也适用于对象,但当前的实现已经足以解决 OP 的问题。

更新

我调整了上面的指令也可以使用对象集合,用法仍然是一样的:

DEMO

(function(ng) {

    'use strict';

    var app = ng.module('options.attribute', []);

    app.directive('optionsAttribute', function($parse) {
        return {
            restrict: 'A',
            compile: function(tElem, tAttr) {

                var transform = $parse(tAttr.optionsAttributeTransform);

                return function(scope, elem, attr) {

                    scope.$watch(attr.optionsAttribute, function(collection) {

                        var options = elem.children();

                        collection = new Collection(collection);

                        options = Array.prototype.slice.call(options, options.length - collection.getLength());

                        ng.forEach(options, function(option, index) {

                            var newAttributes = transform(scope, {
                                $data: collection.getItem(index),
                                $label: option.innerHTML,
                                $index: collection.getIndex(index)
                            });

                            var optionElement = angular.element(option);

                            ng.forEach(newAttributes, function(value, attribute) {

                                optionElement.attr(attribute, value);

                            });

                        });

                    }, true);

                };

            }
        };
    });

    function Collection(collection) {

        var self = this;
        var indexes;

        if(angular.isObject(collection)) {

            if(Object.keys) {
                indexes = Object.keys(collection);
            } else {
                ng.forEach(collection, function(item, index) {
                    indexes.push(index);
                });
            }

            self.getItem = function(index) {
                return collection[indexes[index]];
            };

            self.getLength = function() {
                return indexes.length;
            };

            self.getIndex = function(index) {
                return indexes[index];
            };

        } else if(angular.isArray(collection)) {

            self.getItem = function(index) {
                return collection[index];
            };

            self.getLength = function() {
                return collection.length;
            };

            self.getIndex = function(index) {
                return index;
            };

        }

    }


})(angular);