1

I have a custom directive, and also I have a controller in a separate file. I know I can add a controller to this directive in this way

   .directive("languageFooter", function () {
        return {
            restrict: "E",
            templateUrl: "templates/footer.html",
            controller: ['$scope', function($scope) {..}
        };
    });

But I was wondering if it's possible to add a controller that I have in a separate file something like this

     .directive("languageFooter", function () {
        return {
            restrict: "E",
            templateUrl: "templates/footer.html",
            controller: "customController"
        };
    });
4
  • 3
    ofcourse you can add this way also.... All you have to do is load the controller file before the directive file... Commented Nov 19, 2015 at 5:28
  • 2
    Even second approach is more recommended, as you are not defining controller inline, thus it can be reused. Commented Nov 19, 2015 at 5:39
  • Thanks Guys, it was pretty simple Commented Nov 19, 2015 at 22:46
  • @Alex Can you tell how and from where does customController get called? As in where is it supposed to be stored? Like a function or does it need to be defined with app.controller(customController.... ? Commented Sep 8, 2021 at 15:30

1 Answer 1

1

You can ofcourse add controller this way. only thing is that you should load the controller js file before directive js file. Also as per Kulbhushan comment, the second approach is more in practice

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

Comments

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.