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

        };}
        ]);
  • Why do you need to call a directive from service?. You can inject a service into directive but not directive into service – mggSoft Aug 21 '15 at 08: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. – Jaskaran Bal Aug 21 '15 at 08:34
  • Inject service into controller. See how: http://stackoverflow.com/questions/16876748/service-injection-into-controller-with-angularjs – mggSoft Aug 21 '15 at 08:43

2 Answers2

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>
mggSoft
  • 992
  • 2
  • 20
  • 35
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.

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63