angularjs条件指令只能在电脑上 [英] angularjs conditional directive only on desktop

查看:118
本文介绍了angularjs条件指令只能在电脑上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个指令的元素只有当它不存在来自移动查看。因为它是一个外部维护的插件,我不希望修改指令本身。什么是这样做的最简单的方法?

I want to put a directive on an element only if it's not viewed from mobile. As it's an externally maintained plugin I don't want to modify the directive itself. What's the easiest way of doing this?

推荐答案

创建执行检测(或接收它)指令,加指令,如果不从手机来看,从元素删除自身,然后编译元素。

Create a directive that performs the detection (or receives it), adds the directive if not viewed from mobile, removes itself from the element and then compiles the element.

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);
      };
    }
  };
});

演示: http://plnkr.co/编辑/ nJntmfiLZ20JCdSWiRDQ?p = preVIEW

这篇关于angularjs条件指令只能在电脑上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆