Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile directives via service in angularjs

I'm trying to compile directive via angular service but unfortunately it doesn't work. The idea is to show errors in popups.

I've modified $exceptionHandler service:

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

The popup service looks like this:

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

And I don't think that this was a good idea to hard code $compile service (but I haven't any ideas how to realize this in angular):

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

Popup directive:

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

May be there are some other ways to do this? Thanks.

like image 857
user2573863 Avatar asked Jul 11 '13 18:07

user2573863


People also ask

How AngularJS is compiled?

Compiler is an AngularJS service which traverses the DOM looking for attributes. The compilation process happens in two phases. Compile: traverse the DOM and collect all of the directives. The result is a linking function.

What is compile directive?

A compiler directive is an instruction to the compiler to complete a task before formally starting to compile the program, thus they are sometimes called pre-processor directives. Among other items, during the pre-processor step the compiler is looking for compiler directives and processes them as they are encountered.

What is the difference between link and compile in AngularJS?

Link - Programmatically modify resulting DOM element instances, add event listeners, and set up data binding. Compile - Programmatically modify the DOM template for features across copies of a directive, as when used in ng-repeat.

What do the services represent in AngularJS What are directives?

Service in AngularJS is a function or an object that can be used to share data and the behaviour across the application (controller, directives, filters, other services etc.) or we can say services in AngularJS are objects that are wired together using DI (dependency injection) and it can be used to share and organize ...


1 Answers

You can inject $compile directly into your service, also you're not quite using $compile correctly:

//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
//}]);
like image 137
Noah Freitas Avatar answered Oct 06 '22 00:10

Noah Freitas