简体   繁体   中英

right way to add dependency into a angular.module

var myTestApp=angular.module('myApp',[]);
myTestApp
  .controller('MyController',
    ['$scope', 'EmailParser',
      function($scope, EmailParser) {
    ...}]);

angular.module('myApp', ['emailParser'])
  .controller('MyController',
    ['$scope', 'EmailParser',
      function($scope, EmailParser) {...}]);

I will encounter error when I use above method to add the dependency of emailParser into an agular module. What is the right way to do it? NOTE: emailParser is declared in the actual code

Update

I don't want certain controller have access to email Parser module. I am not sure if this kind of thinking is right. What is the best practice for adding dependency after the declaration of module at the beginning? Am I going the right way to do it?

The error I received was

Error: [ng:areq] Argument 'MyController' is not a function, got undefined

I am sure if my way of thinking how it work is right. Is that I want add emailParser into the module not a good idea, or not a good practice?

Thank You

You must add dependencies when you first declare your module. Any reason just this isn't working:

angular.module('myApp', ['emailParser'])
  .controller('MyController',
    ['$scope', 'EmailParser',
      function($scope, EmailParser) {...}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM