2014-05-15 3 views
1

모바일에서 볼 수없는 경우에만 지시문을 요소에 넣고 싶습니다. 외부에서 유지 관리되는 플러그인이므로 지시문 자체를 수정하고 싶지는 않습니다. 이 작업을 수행하는 가장 쉬운 방법은 무엇입니까?angularjs 데스크탑에서만 조건부 지시문

+0

이 장치 감지 질문의 일부 또는 당신이 이미 가지고 있습니까? – tasseKATT

+0

@ tasseKATT 나는 이미 그걸 가지고있다.하지만 너가 그것을 언급했다면 나는 너가 그것도 해키라고 생각하면 그것을 고맙게 여길 것이다. – Harry

답변

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

데모 :http://plnkr.co/edit/nJntmfiLZ20JCdSWiRDQ?p=preview