Vai*_*gle 5 angularjs ng-class
我正在尝试使用ng-class加载'class'指令.但是当我这样做时,我的指令永远不会被加载.该指令是一个多用途指令,我不想在此创建一个孤立的范围.它将仅在需要时加载,基于ng-class条件,因此不使用attribute或element指令.有没有人试过这样做并成功了?
使用此指令使用<div ng-class="someClass {{tooltip: enabled}}"></div>
此处enabled是范围变量.
app.directive('tooltip', ['$timeout', '$location', '$rootScope', function (timer, $location, $rootScope) {
return {
restrict: 'C',
transclude: true,
link: function (scope, element) {
var printContent = function () {
/* uses the content of .tooltip-content if it is a complex html tooltip,
otherwise
you can use the title attribute for plaintext tooltips
*/
var tooltipContent = $(element).find('.tooltip-content').html();
if (!tooltipContent) {
tooltipContent = $(element).attr('title');
}
$(element).tooltip({
content: tooltipContent,
items: "img, a, span, button, div",
tooltipClass: "tooltip",
position: { my: "left+30 top", at: "right top", collision: "flipfit" },
show: { effect: "fadeIn", duration: "fast" },
hide: { effect: "fadeOut", duration: "fast" },
open: function (event, ui) { $rootScope.tooltipElement = event.target; }
});
};
timer(printContent, 0);
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
有趣的问题。您似乎不想使用 ng-class 指令,因为它在添加类后不会重新编译内容。您可能想要创建自己的动态类指令,该指令在值为 true 时实际重新编译:
app.directive('dynamicClass', function($compile) {
return {
scope: {
dynamicClassWhen: '=',
dynamicClass: '='
},
link: function(scope, elt, attrs) {
scope.$watch('dynamicClassWhen', function(val) {
if (val) {
console.log(val);
elt.addClass(scope.dynamicClass);
$compile(elt)(scope);
}
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
您可能需要修改它才能删除该类,并取决于它是否$compile足以满足您的需求或者您是否需要进一步操作 html,但这似乎是适合您的正确途径。我在行动中对此进行了摆弄。
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
1660 次 |
| 最近记录: |