Custom directives on AngularJS pages without a special set of ng-app modules

I have several simple pages that do not require a special application module, which must be specified in the attribute ng-app. But these pages also use some of my custom directives.

It seems to me that I put all my directives in a separate namespace (namely the module), i.e. MyApp.Directives.

This is great when I also provide my application module, because I add MyApp.Directivesas a dependency, and it works.

angular.module("MyApp", ["MyApp.Directives", ...])

But . As said, I also have very simple pages that really don't require any particular application module, because they don't need any user controllers or anything else. They are simply driven by attributes ng-.../ directives.

Question

I know that I can just add all my custom directives to the module ngand they will become available for all pages. Those who have a custom application module, and those who do not. But this exceeds the purpose of the modules, so I wonder if there is another way to tell the dependency injectors of my additional directives / filters?

, . ( AngularJS). , , , , ng , ng... Angular, , , .

manully angular.module("ng").requires, .

+4
1

# 1) , ngApp:

<html ng-app="MyApp.Directives">

# 2) , angular.bootstrap :

angular.element(document).ready(function(){
  angular.bootstrap(document,['MyApp.Directives','MyApp.Filters']);
});

# 3) :

<html ng-app="myApp">

......

<script>
  angular.module('myApp',['MyApp.Directives','MyApp.Filters']);
</script>

- :

<html ng-app="MyApp.Directives  MyApp.Filters">

:

function angularInit(element, bootstrap) {

// some code

  if (appElement) {
    bootstrap(appElement, module ? module.split(/\s+/) : []); // split by spaces :)
  }
}

: http://plnkr.co/edit/kSrY3WYzLG39NJ4UgTRM?p=preview

+3

Source: https://habr.com/ru/post/1526347/


All Articles