Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 67141

Force ng-strict-di globally not just on an ng-app level

I'm trying to add ng-strict-di (to help throw errors anytime DI isn't done correctly for a controller/directive/etc).

I have a globalApp module containing miscellaneous helper directives / httpInterceptors etc. Is it possible to add it there somehow?

I found this solution, but this module never gets bootstrapped to the page, it's simply Injected into a child Module on a page.

angular.bootstrap(document, ['globalApp'], {
   strictDi: true
});

But this simply breaks, since the page is already bootstrapped with an ng-app="app" on each specific page using angular.

How else can this be achieved?

Upvotes: 2

Views: 1621

Answers (1)

New Dev
New Dev

Reputation: 49590

When you add ng-strict-di to the app, it forces all of the app's dependencies (that are used) to be explicitly annotated.

So, if you have:

<body ng-app="app">
  <foo></foo>
</body>

with foo defined in another module and without explicit annotation:

anotherModule.directive("foo", function(){
  return {
    template: "{{foo}}",
    controller: function($scope){
      $scope.foo = "foo"; // $scope is not explicitly annotated
    }
  };
});

then this would throw an error.

In other words, you don't need to add ng-strict-di per module - just on the app that uses the services, controllers, directives, etc... in that module.

Upvotes: 2

Related Questions