Great question! When making decisions like these, it's important to prioritize separating concerns. This means avoiding calling a method on the parent scope because it can lead to the directive knowing too much about its parent.
Consider the purpose behind your choice. Callbacks are essentially methods. Using `&
` allows for evaluating an expression within the parent scope, while bi-directional binding simply involves a variable name. The power of `&
` lies in its flexibility, as it enables users to not only call functions but also work with any AngularJS expression.
<my-dir cb="callMe()"></my-dir>
Furthermore, directives can respond to changes in state. For example, you can check a condition like this:
<my-dir cb="myVar"></my-dir>
In this scenario, `myVar
` can hold any value, and the directive will react accordingly when it changes. Instead of sharing a variable, you're actually sharing an expression. If the directive doesn't need to alter that variable, why use two-way binding?
Moreover, consider using expressions that evaluate to true or false:
<my-dir cb="myVar == myOtherVar"></my-dir>
The directive doesn't need to know how the parent scope calculates a value, only what the final value is. This approach allows for flexible reactions between the directive and its parent with just simple expressions.
Therefore, `=
` focuses on data binding to keep the scope and directive synchronized on specific variables. On the other hand, `&
` permits evaluation of expressions within the parent scope, enabling both the directive and parent to react to complex state changes effectively.