DoCheck class

Implement to execute ngDoCheck and implement your own change detection.

By default, AngularDart does an identity based change detection of all input bindings (@Input()). That is, for an @Input() Object a, if the expression identical(oldA, newA) is false, AngularDart will invoke comp.a = newA.

When DoCheck is implemented, AngularDart will always invoke comp.a =, meaning that the field (or setter) may get called multiple times with the exact same object. This is used by NgFor, NgStyle and NgClass, for example, in order to update when the contents of the passed in collection (either Iterable or Map) changes, not necessarily just if the identity changes.

WARNING: It is invalid to trigger any asynchronous event in ngDoCheck. Doing so may cause an infinite loop, as DoCheck will continue to be called and the asynchronous events will invalidate the state.

WARNING: It is invalid to implement both DoCheck and OnChanges or AfterChanges. ngOnChanges and ngAfterChanges will never be called, as ngDoCheck is used instead of the default change detector.

Examples

Try this live example from the Lifecycle Hooks page:

void ngDoCheck() {
  if (hero.name != oldHeroName) {
    changeDetected = true;
    changeLog.add(
        'DoCheck: Hero name changed to "${hero.name}" from "$oldHeroName"');
    oldHeroName = hero.name;
  }

  if (power != oldPower) {
    changeDetected = true;
    changeLog.add('DoCheck: Power changed to "$power" from "$oldPower"');
    oldPower = power;
  }

  if (changeDetected) {
    noChangeCount = 0;
  } else {
    // log that hook was called when there was no relevant change.
    var count = noChangeCount += 1;
    var noChangeMsg =
        'DoCheck called ${count}x when no change to hero or power';
    if (count == 1) {
      // add new "no change" message
      changeLog.add(noChangeMsg);
    } else {
      // update last "no change" message
      changeLog[changeLog.length - 1] = noChangeMsg;
    }
  }

  changeDetected = false;
}
Implementers

Constructors

DoCheck()

Properties

hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

ngDoCheck() → void
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator. [...]
inherited