Why did you put the controller in a directive with AngularJS?

I recently saw this example. This is the first time I have seen a controller inside a directive. This is a normal thing. I thought you should keep these two in different areas for verification:

myModule.directive('myComponent', function(mySharedService) {
    return {
        restrict: 'E',
        controller: function($scope, $attrs, mySharedService) {
            $scope.$on('handleBroadcast', function() {
                $scope.message = 'Directive: ' + mySharedService.message;
            });
        },
        replace: true,
        template: '<input>'
    };
})
+5
source share
1 answer

As a rule, you should use controllers in directives in order to be able to share them between directives, on an element. This allows directives to easily exchange information between them.

See here for a detailed explanation of how this works: http://egghead.io/video/angularjs-directive-to-directive-communication/

+5
source

All Articles