如何在AngularJS中的模板属性中调用服务中的指令?

时间:2015-08-21 07:14:53

标签: javascript angularjs angularjs-directive angularjs-service

以下是指令代码和服务代码。如何在服务中调用指令。以及如何调用动态模板

指令代码

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

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

服务代码

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

        };}
        ]);

2 个答案:

答案 0 :(得分:0)

将模板置于指令之外:

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>

答案 1 :(得分:0)

我处于完全相同的情况 - 需要一个模态对话服务,并希望以更加Angular的方式进行,而不是让服务注入DOM。

我能找到的最佳方法是让模态服务创建自己的控制器,如this post中所述。