AngularJS - Dependency injection in services, factories, filters etc

前端 未结 3 2010
孤城傲影
孤城傲影 2021-01-30 02:08

So I have some plugins and libraries I want to use in my angular app and (currently) I am simply referencing those functions/methods as they were intended in 99% of apps in a wa

3条回答
  •  清歌不尽
    2021-01-30 02:39

    While the already existing answers are correct and working, john papas angular style guide favors the use of the $inject service in Y091:

    Filter:

    app.filter('

    Directive:

    app.directive('', MyDirective);
    MyDirective.$inject = ['$http'];
    function MyDirective() {
      return {
        ...
      }
    }
    

    Factory:

    app.factory('', MyFactory);
    MyFactory.$inject = ['$http'];
    function MyFactory() {
      var shinyNewServiceInstance;
      return shinyNewServiceInstance;
    }
    

    Service:

    app.service('', MyService);
    MyService.$inject = ['$http'];
    function MyService() {
      this.foo = foo;
      function foo(){
        ...
      }
    }
    

提交回复
热议问题