18

I have a simple code:

define(['app'], function(app)
{
    app.factory('factoryProvider', function(){
        return {
            name: 'my Name'
        }
    });

    app.directive('myDiv',['factoryProvider', function(factoryProvider) {
        return {
            restrict: 'E',
            replace: true,
            templateUrl: 'link/to/template.html',
            controller: function($scope) {
            },
            link: function(scope, routeParams, location) {
                console.log(factoryProvider.name);
            }
        };   
    }])
});

I want to be able to access myFactorywithin the link function, but I can't! I also tried link: function(scope, routeParams, location, factoryProvider) and that also didn't work. Why?

1 Answer 1

18

It should already be available inside the link function

app.factory('factoryProvider', function(){
    return {
        name: 'my Name'
    }
});

app.directive('myDiv',['factoryProvider', function(factoryProvider) {
    return {
        restrict: 'E',
        replace: true,
        template: '<p>{{name}}</p>',
        controller: function($scope) {
        },
        link: function(scope) {
            scope.name=factoryProvider.name;
        }
    };
}]);
Sign up to request clarification or add additional context in comments.

9 Comments

Well, it is defined for me, but when I get parameters returned, it says undefined. But the exact same method returns the parameter when used inside the controller.
It says can't find method of undefined or that the method is undefined? If factoryProvider is defined, what is it?
No, the returned value is undefined. When I log the factory inside link, it only shows a constructor. When I log it inside the controller, it shows all methods as well.
Uh idk, maybe the "Provider" is messing you up, try renaming your facotry
Haha, no it didn't! I changed it to randomNameTest and it still didn't work :p
|

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.