[英]How to pass a value to an Angular Directive through the class attribute?
I have written the following directive that creates a twitter card when you pass the id of the tweet. 我写了以下指令,当你传递推文的id时创建一个推特卡。
angular.module('app')
.directive('tweetCard',function () {
return {
transclude:true,
template: '<ng-transclude></ng-transclude>',
restrict: 'AEC',
controller:function($scope, $element, $attrs){
twttr.widgets.createTweet($attrs.tweetId,$element[0],
{theme:$attrs.theme?$attrs.theme:'light'})
.then(function(){
$element.find('ng-transclude').remove();
});
}
};
});
This directive works great if I use it as beneath. 如果我在下面使用它,这个指令很有效。
<tweet-card tweet-id="639487026052644867"></tweet-card>
<div tweet-card tweet-id="639487026052644867"></div>
Though, the reason I created this directive is so I could put this tag into my wordpress.com blog. 虽然,我创建此指令的原因是我可以将此标记放入我的wordpress.com博客。 After trying it, it seems wordpress doesn't allow unkown tags, which I expected.
在尝试之后,似乎wordpress不允许未知的标签,这是我所期望的。 But they also don't allow unknown attributes, or data-* attributes in a post.
但是它们也不允许在帖子中使用未知属性或data- *属性。 So I tried to put everything in the class attribute as you see below.
所以我尝试将所有内容放在class属性中,如下所示。
<div class="tweet-card tweet-id:639526277649534976;"></div>
Unfortunately, this doesn't work and I tried fiddling with it. 不幸的是,这不起作用,我试图摆弄它。 I can extend the directive to also check if the tweetCard attribute contains the id like this.
我可以扩展指令以检查tweetCard属性是否包含这样的id。
angular.module('app')
.directive('tweetCard',function () {
return {
transclude:true,
template: '<ng-transclude></ng-transclude>',
restrict: 'AEC',
controller:function($scope, $element, $attrs){
var id = $attrs.tweetId?$attrs.tweetId:$attrs.tweetCard;
twttr.widgets.createTweet(id,$element[0],
{theme:$attrs.theme?$attrs.theme:'light'})
.then(function(){
$element.find('ng-transclude').remove();
});
}
};
});
With the following html. 用以下的html。
<div class="tweet-card:639526277649534976;"></div>
Though, I don't like this workaround, and I can't pass another attribute like the theme attribute. 虽然,我不喜欢这种解决方法,但我无法传递像theme属性这样的其他属性。 Anyone an idea how to pass multiple variables through the class attribute to a directive?
任何人都知道如何通过class属性将多个变量传递给指令?
I looked at the AngularJS docs for a way to use multiple variables via class but found nothing so I wrote a function to convert the class name in the syntax angular reads. 我查看了AngularJS文档,了解了一种通过类使用多个变量的方法,但没有找到任何内容,因此我编写了一个函数来转换语法角度读取中的类名。 (
<span class="my-dir: exp;"></span>
) to an object. (
<span class="my-dir: exp;"></span>
)到一个对象。
function classNameToObj(className) {
//different attributes are separated by semicolons
var attributes = className.split(';');
var obj = {};
for (var i = 0; i < attributes.length; i++) {
var attribute = attributes[i];
//key-values separated by colon
var splittedAttr = attribute.split(':');
obj[splittedAttr[0].trim()] = splittedAttr[1].trim();
}
return obj;
}
This way your HTML can pass both the tweet ID and the theme: 这样您的HTML就可以传递推文ID和主题:
<div class="tweet-card:639526277649534976; theme:dark"></div>
And your directive can create the widget like this: 你的指令可以像这样创建小部件:
var id = $attrs.tweetCard;
var attributes = classNameToObj($attrs.class);
var theme = attributes.theme;
twttr.widgets.createTweet(id, $element[0], {
theme: theme || 'light'
})
.then(function() {
$element.find('ng-transclude').remove();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.