Tôi biết đây là một câu hỏi cũ, nhưng google đã đưa tôi đến đây và tôi không thích câu trả lời ở đây ... Chúng dường như thực sự phức tạp đối với một thứ đơn giản. Vì vậy, tôi đã tạo ra chỉ thị này:
***** NỘI DUNG MỚI *****
Tôi đã kể từ khi làm chỉ thị này chung chung hơn, hỗ trợ một phân tích cú pháp (giá trị góc điển hình) "thuộc tính" thuộc tính.
/**
* Author: Eric Ferreira <http://stackoverflow.com/users/2954747/eric-ferreira> ©2016
*
* This directive takes an attribute object or string and adds it to the element
* before compilation is done. It doesn't remove any attributes, so all
* pre-added attributes will remain.
*
* @param {Object<String, String>?} attributes - object of attributes and values
*/
.directive('attributes', function attributesDirective($compile, $parse) {
'use strict';
return {
priority: 999,
terminal: true,
restrict: 'A',
compile: function attributesCompile() {
return function attributesLink($scope, element, attributes) {
function parseAttr(key, value) {
function convertToDashes(match) {
return match[0] + '-' + match[1].toLowerCase();
}
attributes.$set(key.replace(/([a-z][A-Z])/g, convertToDashes), value !== undefined && value !== null ? value : '');
}
var passedAttributes = $parse(attributes.attributes)($scope);
if (passedAttributes !== null && passedAttributes !== undefined) {
if (typeof passedAttributes === 'object') {
for (var subkey in passedAttributes) {
parseAttr(subkey, passedAttributes[subkey]);
}
} else if (typeof passedAttributes === 'string') {
parseAttr(passedAttributes, null);
}
}
$compile(element, null, 999)($scope);
};
}
};
});
Đối với trường hợp sử dụng của OP, bạn có thể làm:
<li ng-repeat="color in colors">
<span attributes="{'class': color.name}"></span>
</li>
Hoặc sử dụng nó như một chỉ thị thuộc tính:
<li ng-repeat="color in colors">
<span attributes="color.name"></span>
</li>
***** END NỘI DUNG MỚI ** ****
/**
* Author: Eric Ferreira <http://stackoverflow.com/users/2954747/eric-ferreira> ©2015
*
* This directive will simply take a string directive name and do a simple compilation.
* For anything more complex, more work is needed.
*/
angular.module('attributes', [])
.directive('directive', function($compile, $interpolate) {
return {
template: '',
link: function($scope, element, attributes) {
element.append($compile('<div ' + attributes.directive + '></div>')($scope));
}
};
})
;
Đối với trường hợp cụ thể trong q này Question, người ta chỉ có thể viết lại các chỉ thị một chút để làm cho nó áp dụng các chỉ thị để một khoảng bằng lớp, như vậy:
angular.module('attributes', [])
.directive('directive', function($compile, $interpolate) {
return {
template: '',
link: function($scope, element, attributes) {
element.replaceWith($compile('<span class=\"' + attributes.directive + '\"></span>')($scope));
}
};
})
;
Sau đó, bạn có thể sử dụng bất cứ nơi nào này và chọn một chỉ thị theo tên động. Sử dụng nó như vậy:
<li ng-repeat="color in colors">
<span directive="{{color.name}}"></span>
</li>
Tôi cố tình giữ chỉ thị này đơn giản và dễ hiểu. Bạn có thể (và có lẽ sẽ) phải reword nó để phù hợp với nhu cầu của bạn.
câu hỏi thú vị! – TheHippo
Tôi không chắc chắn điều đó là có thể. Tuy nhiên, bạn có thể chuyển color.name thành tham số cho một chỉ thị duy nhất, sau đó kiểm tra giá trị và chạy/gọi mã thích hợp từ đó. – mikel