How to share a single Directive across multiple modules in AngularJS

后端 未结 3 1438
[愿得一人]
[愿得一人] 2021-01-30 22:11

As far as I know about AngularJS, this is one of the interesting libraries out today. I am slowly understanding the Angular power, and I am seriously loving it. I have a few dou

3条回答
  •  面向向阳花
    2021-01-30 23:02

    The easiest way

    The easiest way is to add all sharable directives and other parts to ng module.

    angular
        .module("ng")
        .directive("myDirective", function() {
            ...
        })
        .filter("MyFilter", function() {
            ...
        });
    

    This is the easiest way (a kind of set and forget) as you can easily just drop your directive script to your HTML and forget about it while developing new modules that naturally also require it.

    What else is good about this technique

    Why is adding shareable stuff to ng module sensible? Because you may have simple pages in your app that don't define a particular ngApp module, but rather just set ng-app on HTML/BODY and run inline angular directives only.

    When you add your directive to ng module, these pages can also use your directive.

    
        
            
    ...

提交回复
热议问题