2016-06-08 6 views
0

그래서 속성이나 요소가 될 수있는 지시어가 있습니다. 속성 일 때 선언 된 기존 요소 안에 템플릿을로드하고 싶지 않습니다. 요소 일 때 주어진 템플릿을로드하고 지시문의 사용자 정의 태그를 채 웁니다.선택적인 각도 지시문 templateUrl

나는 시도했다 :

return { 
    link: function(){...}, 
    templateUrl: function(element, attrs){ 
    url = "path/to/directive/template.html"; 

    // Checking if directive is used as an attribute here 
    if(angular.isDefined(attrs.myDirective)){ 
     url = null; // Tried false, empty string, etc. but angular not happy with any of it 
    } 
    return url; 
    } 
} 

어떤 생각이 어떻게 이것을 달성하기 위해?

답변

0

동일한 이름을 가진 2 개의 지시문을 작성하고 restrict를 사용하여 다른 템플릿 동작을 지정했다고 생각하십니까?

function directiveFactory(usesTemplate){ 

    return ['$timeout', function($timeout){ 
     var ddo = { 
      link: function(scope, el,attr){ 
       $timeout(someThing, 1000) 
       //if you dont actually need to use a timeout, might 
       //i suggest scope.$applyAsync if your version of angular supports it? 
      } 
     } 

     if(usesTemplate){ 
      ddo.restrict = 'E'; 
      ddo.templateUrl = 'path/to/template'; 
     } else { 
      ddo.restrict = 'A'; 
     } 

     return ddo; 
    }]; 

} 

module('somename').directive('someName', directiveFactory(true)).directive('someName', directiveFactory(false)); 
+0

두 지침에 동일한 이름을 사용할 수 있다고 생각하지 마십시오. – charlietfl

+0

@charlietfl : 할 수 있습니다. 기존 지시문에 새로운 동작을 추가하는 데 적합합니다. (예를 들어, 모든 ng-click 활성화를 로깅) – Iain

+0

답장을 보내 주셔서 감사합니다. 문제는 제가 링크 기능 내에서 앵글 공급 업체를 사용하고 있다는 것입니다. 그리고 이러한 공급자는 .directive() 함수에서 선언됩니다. '.directive ('myDirective', [ '$ timeout', function ($ timeout) {}])'와 같은 형식을 사용합니다. 그리고 링크 함수가 외부에 있으면 공급자가 정의되지 않았기 때문에 오류가 발생합니다. – MonsieurNinja