0

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?

Martijn
  • 24,441
  • 60
  • 174
  • 261
  • Sounds like the digest cycle has not run completely. Consider usage of a watcher and a shared service – Wottensprels Jan 02 '14 at 13:59
  • 1
    Please post your code in Plunker or Fidddle – Maxim Shoustin Jan 02 '14 at 14:01
  • possible duplicate of [Why is $timeout needed when dynamically nesting ng-form in Angular to ensure child form links with parent form?](http://stackoverflow.com/questions/30948066/why-is-timeout-needed-when-dynamically-nesting-ng-form-in-angular-to-ensure-chi) – Paul Sweatte Sep 09 '15 at 15:26

0 Answers0