Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two way data binding in AngularJS Directives

I have been trying to define directives so I can display different "widgets" in a form, depending on the type of field and its parameters, which are stored in a database. I need to react to different types of scenarios, hence the need for directives to handle layout.

While playing with a few examples, I came up with a code that *kinda* works:

HTML

<input type="text" ng-model="myModel" style="width: 90%"/>   <div class="zippy" zippy-title="myModel"></div> 

Directive

myApp.directive('zippy', function(){     return {       restrict: 'C',       // This HTML will replace the zippy directive.       transclude: true,       scope: { title:'=zippyTitle' },       template: '<input type="text" value="{{title}}"style="width: 90%"/>',       // The linking function will add behavior to the template       link: function(scope, element, attrs) {             // Title element             element.bind('blur keyup change', function() {                 scope.$apply(read);             });              var input = element.children();               function read() {                 scope.title = input.val();             }         }     } }); 

This seems to works (albeit noticeably slower than a *proper* angularJS variable binding) but I figure there must be a better way to do this. Can anyone shed some light on the matter?

like image 602
Tiago Roldão Avatar asked Nov 08 '12 17:11

Tiago Roldão


People also ask

Which directive is used for two way bindings?

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.

Does AngularJS support two way binding?

AngularJS creates a two way data-binding between the select element and the $ctrl.

Which directive is used for data binding in AngularJS?

A - ng-bind directive binds the AngularJS Application data to HTML tags.

What are two way data binding?

Two-way binding means that any data-related changes affecting the model are immediately propagated to the matching view(s), and that any changes made in the view(s) (say, by the user) are immediately reflected in the underlying model. When app data changes, so does the UI, and conversely.


1 Answers

I don't know why you are triggering the $apply method manually because you actually don't need it.

I edited the example you used from the Angular page and included the input. It works for me: http://jsfiddle.net/6HcGS/2/

HTML

<div ng-app="zippyModule">   <div ng-controller="Ctrl3">     Title: <input ng-model="title">     <hr>     <div class="zippy" zippy-title="title"></div>   </div> </div>​ 

JS

function Ctrl3($scope) {   $scope.title = 'Lorem Ipsum'; }  angular.module('zippyModule', [])   .directive('zippy', function(){     return {       restrict: 'C',       replace: true,       transclude: true,       scope: { title:'=zippyTitle' },       template: '<input type="text" value="{{title}}"style="width: 90%"/>',       link: function(scope, element, attrs) {         // Your controller       }     }   }); 

UPDATE maxisam is right, you have to use ng-model instead of binding the variable against the value like so:

<input type="text" ng-model="title" style="width: 90%"/> 

Here is the working version: http://jsfiddle.net/6HcGS/3/

like image 160
F Lekschas Avatar answered Oct 09 '22 19:10

F Lekschas