In angular, how do you provide a controller's argument list from within a directive definition?
-
21-12-2019 - |
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';
},
});
Solution
Same way as usual!
controller: ['$scope', function($scope){...}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow