【发布时间】:2012-08-08 11:23:17
【问题描述】:
我的 AngularJS 模板包含一些自定义 HTML 语法,例如:
<su-label tooltip="{{field.su_documentation}}">{{field.su_name}}</su-label>
我创建了一个指令来处理它:
.directive('suLabel', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
title: '@tooltip'
},
template: '<label><a href="#" rel="tooltip" title="{{title}}" data-placement="right" ng-transclude></a></label>',
link: function(scope, element, attrs) {
if (attrs.tooltip) {
element.addClass('tooltip-title');
}
},
}
})
一切正常,除了attrs.tooltip 表达式,它总是返回undefined,即使在执行console.log(attrs) 时,在谷歌浏览器的JavaScript 控制台中可以看到tooltip 属性。
有什么建议吗?
更新:Artem 提供了一个解决方案。它包括这样做:
link: function(scope, element, attrs) {
attrs.$observe('tooltip', function(value) {
if (value) {
element.addClass('tooltip-title');
}
});
}
AngularJS + stackoverflow = 幸福
【问题讨论】:
-
这个answer 到另一个问题解释了如何在 AngularJS 中正确表达三元组。
-
就是这样:“AngularJS + stackoverflow = bliss”
标签: angularjs angularjs-directive