daviesgeek.com

Angular Service Providers with config

-

Simple example of an Angular service provider with configuration option(s):

var app = angular.module("app", []); app.service('service', function() { var service = function (enabled) { this.enabled = enabled } service.prototype.isEnabled = function() { return this.enabled } return service }) app.provider('MyCoolService', function () { var enabled; return { setEnabled: function (isEnabled) { enabled = isEnabled; }, $get: function (service) { return new service(enabled) } }; }); app.config(function (MyCoolServiceProvider) { MyCoolServiceProvider.setEnabled(true) }); app.controller("Controller", function ($scope, MyCoolService) { $scope.enabled = MyCoolService.isEnabled() });

Working Plunkr