8

Próbuję skompilować dyrektywę za pomocą usługi kątowej, ale niestety to nie działa. Chodzi o to, aby wyświetlać błędy w wyskakujących okienek.Instrukcje kompilacji za pośrednictwem usługi w angularjs

Mam zmodyfikowany $ exceptionHandler usługę:

crm.factory('$exceptionHandler', function(popup) { 
    return function(exception) { 
     popup.open({message: exception}); 
    } 
}); 

podręczne obsługa wygląda tak:

crm.factory('popup', function ($document) { 
    return { 
     open: function (data) { 
      var injector = angular.element(document).injector(), 
       $compile = injector.get('$compile'), 
       template = angular.element('<popup></popup>'); 

      // var ctmp = $compile(template.contents()); 
      $compile(template.contents()); 

      $document.find('body').append(template); 
     } 
    }; 
}); 

I nie sądzę, żeby to był dobry pomysł twardy kod $ compile usługi (ale nie mam pomysłów, jak zrealizować to w kanciastych):

$compile = injector.get('$compile') 

dyrektywa Popup:

crm.directive('popup', function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     templateUrl: '/public/js/templates/common/popup.html', 
     link: function() { 
      console.log('link()'); 
     }, 
     controller: function() { 
      console.log('ctrl()'); 
     } 
    }; 
}); 

Może są jakieś inne sposoby, aby to zrobić? Dzięki.

Odpowiedz

1

można wstrzykiwać $compile bezpośrednio do Twojej dyspozycji, także nie jesteś całkiem za pomocą $compile poprawnie:

//commented alternative lines for allowing injection and minification since reflection on the minified code won't work 
//crm.factory('popup', ['$document', '$compile', function ($document, $compile) { 
crm.factory('popup', function ($document, $compile) { 
    return { 
     open: function (data) { 
      var template = angular.element('<popup></popup>'), 
       compiled = $compile(template); 

      $document.find('body').append(compiled); 
     } 
    }; 
}); 
//closing bracket for alternative definition that allows minification 
//}]); 
+2

nope, nie mogę go używać bezpośrednio. Błąd wygląda następująco: "Uncaught Error: Circular dependency: $ compile <- popup <- $ exceptionHandler <- $ rootScope" – user2573863

+1

Nie działa w Angular 1.2, prosi o zakres jako parametr – perrohunter

+3

, aby naprawić, że można wstrzyknąć '$ rootScope' i wykonaj' $ rootScope. $ new() ', aby utworzyć nowy zakres, zaktualizował odpowiedź. – Less