Is there a way to only rerender the input field I'm typing in, since all other components cannot change
Yes, for the components that will not change, set the change detection strategy for those components to OnPush
. An OnPush
component will then only be checked for changes if
- any of its input properties changes
- it fires an event (e.g., a button click)
- an observable (which is an input property or a local-to-the-component property) fires an event, and
| async
is used in the template with the observable (see plunker in the comments below this answer)
import {Component, Input, ChangeDetectionStrategy} from 'angular2/core';
@Component({
...
changeDetection: ChangeDetectionStrategy.OnPush
})
Also consider listening for changes to your input by subscribing to the valueChanges
Observable Angular makes available on your form element if you use ngFormControl. You can then use debounce()
to only process changes every second or whatever time frame is appropriate:
<input type=text [ngFormControl]="input1Control">
constructor() {
this.input1Control = new Control();
}
ngOnInit() {
this.input1Control.valueChanges
.debounceTime(1000)
.subscribe(newValue => console.log(newValue))
}
See https://mcmap.net/q/49644/-angular-2-and-debounce for a working plunker.