Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get two way data binding in a directive *without* an isolate scope?

Using scope: { ... } in a directive introduces an isolate scope, which does not prototypically inherit from its parent scope. But I have always used it for a different reason: a convenient way to declare HTML attributes with two way data binding:

scope: {
    attr1: '=',
    attr2: '?='
}

To get a non-isolate scope, you have to use scope: true, which does not offer the opportunity to declare such attributes. I now find myself needing a directive with a non-isolate scope, but with two way binding. What's the best way to achieve this?


Example: My use-case is something like this, in the view of the outer-directive:

<div ng-repeat="e in element">
    <inner-directive two-way-attr="e.value"></inner-directive>
</div>

But inner-directive is in the same module as outer-directive. It doesn't need to be encapsulated with an isolate scope. In fact, I need to use $scope inheritance for other purposes, so an isolate scope is not an option. It's just that using an HTML attribute to establish this two-way communication is extremely convenient.

like image 631
mhelvens Avatar asked May 22 '14 10:05

mhelvens


People also ask

Which directive is used for two way data binding?

Two-way data binding can be achieved using a ngModel directive in Angular. The below syntax shows the data binding using (ngModel), which is basically the combination of both the square brackets of property binding and parentheses of the event binding.

Which property value of an isolated scope of a directive creates a bidirectional binding between the local directive scope and parent scope?

In simple words = is used in directive isolate scope to enable two way binding and @ does not updates model, only updates Directive scope values.

Which object is used for two way data binding in AngularJS?

Data binding is the most useful and powerful feature among any of the existing or upcoming software development technologies. It is actually a process that bridges a connection between the view and business logic of the application.

What does two way data binding stands for?

Two-way data binding refers to sharing data between a component class and its template. If you change data in one place, it will automatically reflate at the other end. For example, if you change the value of the input box, then it will also update the value of the attached property in a component class.


1 Answers

The answer by pixelbits helped me figure this out big time, but taken as a direct answer to my original question, it seems overly complicated. After looking into it, the solution is really quite simple.

Take a directive with an isolate scope like this:

scope: { model: '=myModel' },
link: function(scope, element, attr) {
    //...
}

The following is equivalent, except that the scope is not isolate:

scope: true,
link: function(scope, element, attr) {
    scope.model = scope.$parent.$eval(attr.myModel);
    //...
}

See a working example here: http://jsfiddle.net/mhelvens/SZ55R/1/

like image 98
mhelvens Avatar answered Oct 03 '22 17:10

mhelvens