1

Below is the directive code and service code. How to call a directive inside the service.And how to call dynamic templates

Directive Code

angular.module('TestModule').directive('mymodalwindow', function () {
    return {        
        restrict: 'E',       

        template: '<div class="modal-header">' +
    '<h3>{{modalOptions.headerText}}</h3>' +
'</div>' +       
    };
});

Service Code

angular.module('TestModule').service('modalService', ['$modal',
    function ($modal) {
        var modalDefaults = {
            backdrop: true,
            keyboard: true,
            modalFade: true,
            template://How to call the above define directive here?? 1 way is <mymodalwindow></mymodalwindow> But how to pass the directives instead of giving fixed directive name

        };}
        ]);
3
  • Why do you need to call a directive from service?. You can inject a service into directive but not directive into service Commented Aug 21, 2015 at 8:17
  • 1
    I need to invoke the modalService from controller method, which will pop up the dialog modal. and need to replace the template of service with some other template. Commented Aug 21, 2015 at 8:34
  • Inject service into controller. See how: stackoverflow.com/questions/16876748/… Commented Aug 21, 2015 at 8:43

2 Answers 2

0

Takes the template outside the directive:

angular.module('TestModule').directive('mymodalwindow', function () {
    return { 
        restrict: 'E',
        templateURL: 'app/template/myTemplate.html'
    };
});

MyTemplate.html:

<div class="modal-header">
    <h3>{{modalOptions.headerText}}</h3>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

I'm in the exact same situation -- needing a modal dialog service, and wanting to do it in a more Angular way than having a service injecting into the DOM.

Best approach I can find is to have the modal service create its own controller, as outlined in this post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.