Mẫu AngularJS của tôi chứa một số cú pháp HTML tùy chỉnh như:
<su-label tooltip="{{field.su_documentation}}">{{field.su_name}}</su-label>
Tôi đã tạo một chỉ thị để xử lý nó:
.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');
}
},
}
})
Mọi thứ đều hoạt động tốt, ngoại trừ attrs.tooltipbiểu thức, luôn trả về undefined, mặc dù tooltipthuộc tính hiển thị từ bảng điều khiển JavaScript của Google Chrome khi thực hiện a console.log(attrs).
Bất kì lời đề nghị nào?
CẬP NHẬT: Một giải pháp đã được cung cấp bởi Artem. Nó bao gồm việc thực hiện điều này:
link: function(scope, element, attrs) {
attrs.$observe('tooltip', function(value) {
if (value) {
element.addClass('tooltip-title');
}
});
}
AngularJS + stackoverflow = Bliss