0

I am using the 'ng-strict-di' directive in my application to protect against failing minification. But now I am getting an error on my directives.

Error: [$injector:strictdi] function($scope, element, attrs) is not using explicit annotation and cannot be invoked in strict mode

How do I explicitly add $scope as a dependency to the anonymous controller function? Here is my directive.

var myApp = angular.module("myApp",[]);
myApp.directive('myDirective', function(){
  return{
    restrict: 'A',
    template: '<h4> {{myController.msg}} </h4>',
    controller: function($scope, element, attrs){
      $scope.myController = this;
      this.msg = "Hello world";
    };
});

I tried:

var myApp = angular.module('myApp ', ['$scope'])

But I get this error: Error: [$injector:modulerr] Failed to instantiate module

1 Answer 1

2

You need minification safe syntax for the directive controller:

controller: ["$scope", "element", "attrs", function($scope, element, attrs){
  $scope.myController = this;
  this.msg = "Hello world";
}];

Based on those params tho, I'm guessing you wanted a link function (just a guess):

link: function($scope, element, attrs){
  $scope.myController = this;
  this.msg = "Hello world";
};
Sign up to request clarification or add additional context in comments.

3 Comments

The first part is ok. The choice of link should be a bit more valid than just its arguments. Also, this is undefined in link (irrespective of the fact that $scope.myController = this; is a use case for controllerAs and shouldn't be there at all).
Thanks! I think all need is controller: ["$scope", function ($scope, element, attrs)
@estus -- Yeah, it was an assumption based on the params passed into the controller func

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.