Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ngModel's $render is not called when the model changes in AngularJS?

DEMO

Why in the following example $render is not called when the button is clicked?

<input type="text" ng-model="client.phoneNumber" phone-number>
<button ng-click="client.phoneNumber='1111111111'">Change phone number</button>
.directive("phoneNumber", function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, element, attrs, ngModel) {
      ngModel.$render = function() {
        alert('rendering');   // This is not called
      };
    } 
  };
}); 
like image 833
Misha Moroshko Avatar asked Jan 13 '14 04:01

Misha Moroshko


People also ask

What is $viewValue in AngularJS?

$viewValue is the current value in the view - your string input.

What is $setValidity in AngularJS?

The $setValidity() function is a built-in AngularJS function that is used to assign a key/value combination that can be used to check the validity of a specific model value. The key in this case is “unique” and the value is either true or false.

How do NG models work?

NgModel works using these two bindings together. First, it passes the data from the component class and set data in FormControl. Second, it passes the data from UI after a specific action is triggered and then changes the value of the control.

What are models in AngularJS?

The model in an MVC-based application is generally responsible for modeling the data used in the view and handling user interactions such as clicking on buttons, scrolling, or causing other changes in the view. In basic examples, AngularJS uses the $scope object as the model.


2 Answers

The input directive is running after your directive and thus it's $render function is replacing yours.

Set your directive's priority to something greater than 0. For instance:

.directive("phoneNumber", function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    priority: 1,
    link: function(scope, element, attrs, ngModel) {
      ngModel.$render = function() {
        alert('rendering');
      };
    } 
  };
});

And your $render will take precedence and you'll see your alert is called.

like image 138
KayakDave Avatar answered Oct 02 '22 18:10

KayakDave


ngModel.$render is working, when calling $render in the $apply.

app.directive('changecase', function ($timeout) {
    return {
    restrict: 'A',
    require: 'ngModel',
    priority: 1,
    link: function (scope, element, attrs, ngModel) {
      
        //format text going to user (model to view)
        ngModel.$formatters.push(function(value) {
            return value.toUpperCase();
        });

        //format text from the user (view to model)
        ngModel.$parsers.push(function(value) {
            return value.toUpperCase();
        });

        element.on('blur keyup change', function() {
            scope.$apply(function(){
              ngModel.$setViewValue(ngModel.$modelValue);
              ngModel.$render();
            });
            });
        }
    }
});
like image 28
huzemin Avatar answered Oct 02 '22 18:10

huzemin