在其模板中使用 Angular Directive 属性 [英] Use Angular Directive attributes in its template
本文介绍了在其模板中使用 Angular Directive 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在指令中使用属性的值?我的元素看起来像这样:
我想在指令的模板中使用它,如下所示:
mainApp.directive('myTooltip',功能() {//允许的事件监听器var allowedListeners = ["点击"];返回 {限制:'A',模板:'<div class="tooltip-title">...</div>'+'<div class="tooltip-content">'+'...</div>',链接:函数(范围,榆树,属性){if(allowedListeners.indexOf(attrs.myTooltip) != -1){elm.bind(attrs.myTooltip, function(){...});}}};});
三个点的位置应该有代码,但我不知道如何将 attrs 对象(attrs.tooltipTitle
等)的内容放入该模板中.
解决方案 您可以像这样拉出属性并将它们放入指令的范围内:
angular.module('myApp', []).指令('myTooltip',函数($log){//允许的事件监听器var allowedListeners = ["点击"];返回 {限制:'A',模板:'<div class="tooltip-title">{{tooltipTitle}}</div>'+'<div class="tooltip-content">'+'{{tooltipContent}}</div>',范围: {tooltipTitle: '@tooltipTitle',tooltipContent: '@tooltipContent'},链接:功能(范围,榆树,属性){如果(allowedListeners.indexOf(attrs.myTooltip)!= -1){elm.bind(attrs.myTooltip, 函数 () {$log.info('点击');});}}};});
这是小提琴:http://jsfiddle.net/moderndegree/f3JL3/
How can I use the value of an attribute in a directive? My element looks like this:
<div class="tooltip-icon"
data-my-tooltip="click"
data-tooltip-title="foo"
data-tooltip-content="test content"></div>
I would like to use that in the template of my directive, which looks like this:
mainApp.directive('myTooltip',
function() {
// allowed event listeners
var allowedListeners = ["click"];
return {
restrict: 'A',
template: '<div class="tooltip-title">...</div>' +
'<div class="tooltip-content">' +
'...</div>',
link: function(scope, elm, attrs) {
if(allowedListeners.indexOf(attrs.myTooltip) != -1){
elm.bind(attrs.myTooltip, function(){
...
});
}
}
};
}
);
Where the triple dots are there should be code, but I cannot figure out how to get the contents of the attrs object (attrs.tooltipTitle
, etc) into that template.
解决方案 You can pull the attributes out and place them into the scope of the directive like this:
angular.module('myApp', []).
directive('myTooltip', function ($log) {
// allowed event listeners
var allowedListeners = ["click"];
return {
restrict: 'A',
template: '<div class="tooltip-title">{{tooltipTitle}}</div>' +
'<div class="tooltip-content">' +
'{{tooltipContent}}</div>',
scope: {
tooltipTitle: '@tooltipTitle',
tooltipContent: '@tooltipContent'
},
link: function (scope, elm, attrs) {
if (allowedListeners.indexOf(attrs.myTooltip) != -1) {
elm.bind(attrs.myTooltip, function () {
$log.info('clicked');
});
}
}
};
});
Here is fiddle: http://jsfiddle.net/moderndegree/f3JL3/
这篇关于在其模板中使用 Angular Directive 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
相关文章
- 在其模板中使用Angular Directive属性;
- javascript - angular directive问题;
- 按钮@Directive通过Angular中的@Input绑定禁用属性;
- @Directive v / s @Component in Angular;
- Directive execution order in angular 2;
- 如何在Angular 2模板中使用Django模板?;
- Directive that works as ng-if (Angular 2);
- Using CSS hover property in Angular directive?;
- Angular 中的 @Directive 与 @Component;
- @Directive与@Component在Angular中;
- 在其他 javascript 框架模板中运行 angular 指令;
- angular 可以在玉石模板中使用吗?;
- 如何在Angular 2模板中使用枚举;
- 在Angular模板HTML中使用CSS变量?;
- javascript - angular的Directive传值问题;
- Extending Angular 2 ngModel directive to use observables;
- 模板解析错误:无法绑定到 DIRECTIVE,因为它不是“div"的已知属性;
- Angular2无法绑定到DIRECTIVE,因为它不是element的已知属性;
- 在 angular 2 中使用组件模板的内容;
- 在 angular 2 中使用组件模板的内容;
- 在 Angular 指令模板中使用外部 Javascript 文件;
- 如何在Django模板中使用href属性;
- javascript - angular在directive 如何执行scroll事件;
- @Directive V / S @Component在angular2;
- Angular 4 | How to dynamically inject a directive when click?;
前端开发最新文章
- 为什么Chrome(在Electron内部)突然重定向到chrome-error:// chromewebdata?;
- 错误102(net :: ERR_CONNECTION_REFUSED):服务器拒绝连接;
- 如何解决'重定向已被CORS策略阻止:没有'Access-Control-Allow-Origin'标题'?;
- 如何处理“Uncaught(in promise)DOMException:play()失败,因为用户没有首先与文档交互。”在桌面上使用Chrome 66?;
- 警告:添加非被动事件侦听器到滚动阻塞'touchstart'事件;
- 如何在浏览器中播放.TS文件(视频/ MP2T媒体类型)?;
- 此请求已被阻止;内容必须通过HTTPS提供;
- 资源解释为样式表,但转换为MIME类型text / html(似乎与web服务器无关);
- 通过HTTPS加载页面但请求不安全的XMLHttpRequest端点;
- 拒绝从执行脚本'*',因为它的MIME类型(“应用/ JSON')不是可执行文件,并严格MIME类型检查被启用。;