If the directive will break without a controller, then the directive should determine the required controller. This creates a one-to-one relationship between the directive and the controller.
Suppose we have a Booking directive that needs a BookingController. It is redundant for developers to define both the directive and the controller every time they need to use the reservation directive. Therefore, the directive can define the controller: "BookingController" , and AngularJS will automatically create an instance of this controller when the directive is used.
What if your directive is generic? You have a directive that only processes the formatting of a booking order, but there are many controllers that handle different types of orders. In this case, the directive will not define the controller. It will only determine what it needs "book_number" in the current area. The developer will have to "use" this directive somewhere in the DOM "under" the controller that processes the reservation.
It is best to think of directives as code that publishes the current scope but does not manipulate the current scope. Controllers are code that controls the current scope but does not know how the scope is published. Views (or HTML) are where these two things relate to each other depending on dependencies.
cgTag source share