0

각도 서비스 내에서 지시문을 작성한 다음 해당 지시문이 포함 된 이벤트를 DOM에 삽입하고 컴파일해야합니다. 삽입 및 컴파일이 제대로 작동하는 것처럼 보일 수도 있지만 각도 (컨트롤러, 서비스 또는 어디서나) 등록하지 마십시오.각도 js로 서비스 내부에 지시문을 만드는 방법은 무엇입니까?

다음은이 지침이 제대로 모듈에 invokeQueue에 추가됩니다

var dirs = angular.module('dirs', []); 

dirs.factory('directiveCreator', function($rootScope) { 

    var creator = {}; 

    creator.addDirective = function(config) { 

     dirs.directive(config.name, function() { 
      return { 
       restrict: config.restrict, 
       template: config.template, 
       replace: config.replace, 
      } 
     }); 

     //insert element with the directive above into DOM on this event and compile 
     $rootScope.$broadcast('directive.added'); 

    } 

    return creator; 
} 

내 현재 코드의 단순화 된 버전은, 그러나 전혀 작동하지 않습니다, 내가 뭔가를 놓친 게 뭐죠?

답변

3

당신이 $compileProvider를 캡처하고 동적 새로운 지침을 포함해야 할 때 다음 다음 moduleObj.directive 방법

var dirs = angular.module('dirs', []); 
dirs.config(function($compileProvider){ 
    dirs.compileProvider = $compileProvider; 
}); 

을 사용하는 대신 directive 방법을 사용할 필요가

creator.addDirective = function(config) { 
    dirs.compileProvider.directive(config.name, function() { 
     return { 
      restrict: config.restrict, 
      template: config.template, 
      replace: config.replace, 
     } 
    }); 
    //insert element with the directive above into DOM on this event and compile 
    $rootScope.$broadcast('directive.added'); 
} 

이 게으른로드 , Here은 그런 일을하는데 도움이되는 기사입니다.