问题描述
我想显示一个组合框,其所有选项字体都将不同.
I want to show a combobox whose all option font will be different.
在AngluarJS中使用ng-options
指令填充<select>
标记的选项时,我无法弄清楚如何为每个选项设置字体系列.
When using the ng-options
directive in AngluarJS to fill in the options for a <select>
tag I cannot figure out how to set the font-family for each option.
$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
中的每个选项.
For ng-repeat if i use to apply style="font-family:{{font}}"
. It works for each and every option in the ng-repeat
.
这里是示例:小提琴
<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
中不起作用.指导我任何可能的答案.
I need to know , Why font-family is not works in ng-option
. Guide me any possible answer.
推荐答案
我创建了一个optionsAttribute
指令,该指令可以在ngOptions
呈现选项标签后转换其属性.
I have created an optionsAttribute
directive that can transform the attributes of your option tags after they are rendered by 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);
});
}
});
});
};
}
};
});
用法
- 在
optionsAttribute
指令的ngOptions
指令中提供您提供的数组值.
- Provide the array value you have provided in the
ngOptions
directive in theoptionsAttribute
directive.
例如
<select ng-options="font for font in reportFontload"
options-attribute="reportFontload">
</select>
- 在
optionsAttributeTransform
指令中添加一个回调,该回调返回一个json对象,该对象表示要附加在每个option
s标记中的属性.回调本身具有2个值:
- Add a callback in the
optionsAttributeTransform
directive that returns an json object that represents the attributes to be appended in each of theoption
s tag. The callback itself is provided with 2 values:
$ data -表示#1中提供的数组中的每个项目.
$data - represents each of the item in the array provided in #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>
或替代方法是提供函数回调
or an alternative is to provide a function callback
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的问题.
The directive is only limited towards array, objects excluded, although there is a way to tweak the current directive to also work with objects, but the current implementation is already sufficient for the OP's problem.
更新
我对上面的指令进行了调整,使其也可以用于对象集合,用法仍然是相同的:
I tweaked the directive above to also work with object collections, the usage still says the same:
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);
这篇关于如何为每个选项在ng-option中应用字体家族的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!