Question

I'm trying to follow the best practice of providing an argument list when defining a controller, like this:

angular.module("myApp", [])
  .controller("myCtrl", ["$scope", function ($scope) {
    // stuff accomplished.
  }];

However, I'm not sure how to provide this argument list when the controller is embedded in a directive definition:

angular.module("myApp", [])
  .directive("myDirective", function () {
    return {
      restrict: 'E',
      replace: true,
      template: '<div>{{ name }}</div>',
      controller: function($scope) {
        // stuff accomplished.
        $scope.name = 'Louis';
      },
    });
Was it helpful?

Solution

Same way as usual!

controller: ['$scope', function($scope){...}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top