我想在一个元素上添加一个指令,只要它不是从移动设备上查看的。由于它是一个外部维护的插件,我不想修改指令本身。这是最简单的方法吗?
答案 0 :(得分:2)
创建一个执行检测(或接收它)的指令,如果没有从移动设备中查看,则添加指令,从元素中删除自身,然后编译元素。
HTML:
<div not-on-mobile="external-directive">Hello.</div>
JS:
app.directive('notOnMobile', function($compile) {
// Perform detection.
// This code will only run once for the entire application (if directive is present at least once).
// Can be moved into the compile function if detection result needs to be passed as attribute.
var onMobile = false;
return {
compile: function compile(tElement, tAttrs) {
if (!onMobile) tElement.attr(tAttrs.notOnMobile, '');
tElement.removeAttr('not-on-mobile');
return function postLink(scope, element) {
$compile(element)(scope);
};
}
};
});