Why do I have to use $timeout to update variables in Angular?

121 Views Asked by At

I have a directive in a directive. In the deepest directive I have this:

directive B:

scope.onChange = function () {
    scope.ngModel = scope.selectedItem.Id;

    $timeout(function () {
        scope.changed();
    }, 0);
}

In the directive on top of that directive I have this:

Directive A:

scope.onChange = function () {
    console.log(scope.ngModel);

    scope.changed();
}

The thing is, when I omit the timeout call in directive B, the console logs the 'old' value, but when I use the timeout call, everything works as expected. So my question is, why do I need to use the timeout here?

0

There are 0 best solutions below