How to inject to factory, directive and filter?

The factory, directive, constant, value and filter etc. methods are registered with modules and we can see the way of presenting to all as below. 

You can use dependency injection for services, filters and directives.

    var app = angular.module('DIApp', []);

    // SERVICES.
    app.service("diService", diService);

    var diService= function() {
        this.getCal = function() {
            return (1+10);
        }
    }

    // FACTORY.
    app.factory('diFactory', ['diService', function (diService) {
        // TODO: YOU CUSTOM LOGIC.
         var result = diService.getCal();
    }]);

    //DIRECTIVE.
    app.directive('diDirective', ['diService', function(diService) {
        // TODO: YOU CUSTOM LOGIC.
        var result = diService.getCal();
    }])

    //FILTER.
    app.filter('filterName', ['depService', function(depService) {
        // TODO: YOU CUSTOM LOGIC.
        var result = diService.getCal();
    }]);
   //VALUE
  app.value("value", 'Anil Singh');

 //CONSTANT
  app.constant("constant", 'constant value.');

About Blogger

ANIL SINGH

Hey! I am Anil Singh. I am Active Blogger, Writer and Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

    Blogger Comment
    Facebook Comment

1 comments:

  1. what is the difference between factory and service

    ReplyDelete

SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book