Implementing scope: { ... }
in a directive creates an isolated scope that doesn't inherit from its parent. However, my usual practice has been to utilize this for easily declaring HTML attributes with bi-directional data binding:
scope: {
attr1: '=',
attr2: '?='
}
If you want a non-isolated scope, you must use scope: true
, but this method doesn't allow the declaration of such attributes. Now, I find myself needing a directive with a non-isolated scope that still enables two-way binding. What's the most effective way to accomplish this?
For example, consider the scenario below within the outer-directive
's view:
<div ng-repeat="e in element">
<inner-directive two-way-attr="e.value"></inner-directive>
</div>
Despite being part of the same module as outer-directive
, inner-directive
doesn't require encapsulation with an isolated scope. In reality, I rely on $scope
inheritance for other purposes so isolating the scope isn't viable. Nonetheless, utilizing an HTML attribute for establishing this two-way communication proves to be highly convenient.