Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directive with isolated scope and ng-model

Tags:

I'm trying to write a directive which make use of isolated scope and ngModel directive.

Problem:
When the model is updated in the directive the value of the caller is not getting updated.

HTML:

<test-ng-model ng-model="model" name="myel"></test-ng-model> 

Directive:

app.directive(     'testNgModel', [     '$timeout',     '$log',  function ($timeout, $log) {      function link($scope, $element, attrs, ctrl) {         var counter1 = 0, counter2 = 0;          ctrl.$render = function () {             $element.find('.result').text(JSON.stringify(ctrl.$viewValue))         }          $element.find('.one').click(function () {             if ($scope.$$phase) return;             $scope.$apply(function () {                 var form = angular.isObject(ctrl.$viewValue) ? ctrl.$viewValue : {};                 form.counter1 = ++counter1;                 ctrl.$setViewValue(form);             });         });         $element.find('.two').click(function () {             if ($scope.$$phase) return;             $scope.$apply(function () {                 var form = angular.isObject(ctrl.$viewValue) ? ctrl.$viewValue : {};                 form.counter2 = ++counter2;                 ctrl.$setViewValue(form);             });         });          $scope.$watch(attrs.ngModel, function (current, old) {             ctrl.$render()         }, true)     }      return {         require: 'ngModel',         restrict: 'E',         link: link,         //if isolated scope is not set it is working fine         scope: true,         template: '<div><input type="button" class="one" value="One"/><input type="button" class="two" value="Two"/><span class="result"></span></div>',         replace: true     };  }]); 

Demo: Fiddle

If the isolated scope is not set it works fine: fiddle

like image 723
Arun P Johny Avatar asked Aug 31 '13 10:08

Arun P Johny


People also ask

Which type of directives starts with * ng?

AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

What is Ng isolate scope?

An Isolated scope property can be bind with DOM attributes. Interpolate or attribute sets up a one-way data binding from the Parent scope to the Isolated Scope of Directive. It means if Parent scope does any changes then changes will be reflected to the Isolated scope of Directive.

Which binding strategy symbol in isolated scope means passing this attribute as an string?

All three bindings are ways of passing data from your parent scope to your directive's isolated scope through the element's attributes: @ binding is for passing strings.

What type of directive ng model is?

ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form.


1 Answers

As discussed in the comments, it is generally not recommended to use a child scope (scope: true or scope: { ... }) with ng-model. However, since Arun needs to create additional scope properties, scope: true can be used with an object, not a primitive. This leverages prototypical inheritance, so $parent is not neeed:

<test-ng-model ng-model="someObj.model" ...> 

fiddle

like image 158
Mark Rajcok Avatar answered Sep 28 '22 02:09

Mark Rajcok