6

In my project i have to create dynamic factory in angular js with dynamic factory name like below

  function createDynamicFactory(modId) {
     return myModule.factory(modId + '-existingService', function (existingService) {
        return existingService(modId);
    }); 
}

I want to get new factory with dynamic name , when i called this function. unfortunately this is not working. how can i achieve this? and how to inject in my controller or in directive dynamically?

2
  • Can you offer more insight than "this is not working"? Commented Feb 12, 2014 at 16:30
  • @Marc: hi marc, i mean, if i put the break point at above return statement(return existingService(modId);) the control is not reaching to that break point,though i called createDynamicFactory function. Commented Feb 12, 2014 at 17:01

1 Answer 1

9

You can annotate your service generator like this. It takes the module and extension and then annotates a dependency on the "echo" service (just an example service I defined to echo back text and log it to the console) so the generated service can use it:

makeService = function(module, identifier) {
    module.factory(identifier+'-service', ['echo', function(echo) {
            return {
                run: function(msg) {
                    return echo.echo(identifier + ": " + msg);
                }
            };
        }]);
    };

Then you can make a few dynamic services:

makeService(app, 'ex1');
makeService(app, 'ex2');

Finally, there are two ways to inject. If you know your convention you can pass it in with the annotation as the ext1 is shown. Otherwise, just get an instance of the $injector and grab it that way.

app.controller("myController", ['ex1-service', 
                                '$injector',
                                '$scope',
                                function(service, $injector, $scope) {
    $scope.service1 = service.run('injected.');   
    $scope.service2 = $injector.get('ex2-service').run('dynamically injected');
}]);

Here is the full working fiddle: http://jsfiddle.net/jeremylikness/QM52v/1/

Updated: if you want to create the service dynamically after the module is initialized, it's a few slight changes. Instead of trying to register the module, simply return an annotated array. The first parameters are the dependencies and the last is the function to register:

makeService = function(identifier) {
    return ['echo', function(echo) {
        console.log("in service factory");
            return {
                run: function(msg) {
                    return echo.echo(identifier + ": " + msg);
                }
            };
        }];
    };

Then you get a reference to the array and call instantiate on the $injector to wire it up with dependencies:

var fn = makeService('ex2');
$scope.service2 = $injector.instantiate(fn).run('dynamically injected');

Here's the fiddle for that version: http://jsfiddle.net/jeremylikness/G98JD/2/

Sign up to request clarification or add additional context in comments.

3 Comments

Hi jeremy thanks for the reply, in my case i am calling makeService function dynamically. I updated your plunker here jsfiddle.net/G98JD. it was throwing Unknown provider error
In that case you just need to define your service and use $injector.instantiate to wire it up. Here is a working example: jsfiddle.net/jeremylikness/G98JD/1
instead of looking for dependencies, can I declare these dynamically created factory/services as dependencies, just like $http and other. Ex: App.controller('TestCtrl, ['$http', 'dynamicFactory', function ($http, dynamicFactory) { ... } ]) ? In my case dynamically factory/service name will not change. Basically I've a provider which create other factories/services dynamically.

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.