css - 如何在 ng-option 中为每个选项应用 font-family

标签 css angularjs angular-ui-bootstrap

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

使用 ng-options 时AngluarJS 中的指令用于填写 <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 中不起作用。指导我任何可能的答案。

最佳答案

我创建了一个 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>
  • optionsAttributeTransform 指令中添加一个回调,该回调返回一个 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);
    

    关于css - 如何在 ng-option 中为每个选项应用 font-family,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951405/

    相关文章:

    AngularJS ng-class 无法覆盖 Bootstrap 下拉 CSS 规则

    html - 使用 CSS 使列表居中

    javascript - Angular 在同一工厂内调用工厂函数

    angularjs - 我是否需要下载特定的 js 库才能使用 highcharts-ng 指令,或者它是否已嵌入 highcharts.js 中

    fullcalendar - 如何在工具提示中显示 HTML 格式的内容? (用户界面日历)

    javascript - 单击 JavaScript 更改按钮颜色

    html - Magento HTML 问题 - 侧边栏显示在页面内容下方

    jquery - 使用 JQuery 编辑 CSS 剪辑,并使用一个设置变量作为值之一

    jquery - 尝试为 .css() 向 Jquery 添加更多属性

    javascript - 即使条件为真,使用 ng-if 也不会显示该元素