Samantha J T Star
Samantha J T Star

Reputation: 32788

Do I need to use a scope.$apply inside a directive when setting the model value?

This is related to a question I asked earlier.

link: function (scope, element, attrs, ngModel) {

            $wmdInput.on('keyup', _.debounce(function () {
                rawContent = $wmdInput.val();          // LINE 1
                scope.$apply(function () {             
                    ngModel.$setViewValue(rawContent); // LINE 2
                });
            }, 500));

Inside the link portion of a directive. Is it necessary to set the model value inside a

scope.$apply( function ()

?

Upvotes: 0

Views: 1451

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

I think that in your case you need invoke $apply. $scope.$apply() calls $scope.$digest(). The $digest loops through all watchers on the specific scope

From DOCS:

Note that calling $setViewValue(value) function does not trigger a $digest.

Upvotes: 2

Related Questions