fix(forms): handle control change in NgFormControl
when a new Control instance is bound to the directive, use the new instance, not the old one
This commit is contained in:
@ -8,7 +8,7 @@ import {NgControl} from './ng_control';
|
||||
import {Control} from '../model';
|
||||
import {Validators, NG_VALIDATORS} from '../validators';
|
||||
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
|
||||
import {setUpControl, isPropertyUpdated, selectValueAccessor} from './shared';
|
||||
import {setUpControl, isPropertyUpdated, isControlChanged, selectValueAccessor} from './shared';
|
||||
|
||||
const formControlBinding =
|
||||
CONST_EXPR(new Provider(NgControl, {useExisting: forwardRef(() => NgFormControl)}));
|
||||
@ -70,8 +70,6 @@ const formControlBinding =
|
||||
export class NgFormControl extends NgControl implements OnChanges {
|
||||
form: Control;
|
||||
update = new EventEmitter();
|
||||
/** @internal */
|
||||
_added = false;
|
||||
model: any;
|
||||
viewModel: any;
|
||||
validators: Function[];
|
||||
@ -84,10 +82,9 @@ export class NgFormControl extends NgControl implements OnChanges {
|
||||
}
|
||||
|
||||
onChanges(changes: {[key: string]: SimpleChange}): void {
|
||||
if (!this._added) {
|
||||
if (isControlChanged(changes)) {
|
||||
setUpControl(this.form, this);
|
||||
this.form.updateValidity();
|
||||
this._added = true;
|
||||
}
|
||||
if (isPropertyUpdated(changes, this.viewModel)) {
|
||||
this.form.updateValue(this.model);
|
||||
|
@ -60,6 +60,12 @@ export function isPropertyUpdated(changes: {[key: string]: any}, viewModel: any)
|
||||
return !looseIdentical(viewModel, change.currentValue);
|
||||
}
|
||||
|
||||
export function isControlChanged(changes: {[key: string]: any}): boolean {
|
||||
if (!StringMapWrapper.contains(changes, "form")) return false;
|
||||
var change = changes["form"];
|
||||
return change.previousValue !== change.currentValue;
|
||||
}
|
||||
|
||||
// TODO: vsavkin remove it once https://github.com/angular/angular/issues/3011 is implemented
|
||||
export function selectValueAccessor(dir: NgControl, valueAccessors: ControlValueAccessor[]):
|
||||
ControlValueAccessor {
|
||||
|
Reference in New Issue
Block a user