Open chenyukunJoshHub opened 8 years ago
我这里测下来并不会发生2次。
import {Component} from 'angular2/core';
import {FORM_PROVIDERS,FormBuilder,Control,ControlGroup} from 'angular2/common';
@Component({
selector: 'App',
providers: [FORM_PROVIDERS],
directives: [],
template: `
<form [ngFormModel]="myForm">
<select [ngFormControl]="city">
<option value="1">上海</option>
<option value="2">北京</option>
</select>
</form>
`
})
export class App {
city: Control = new Control('');
myForm:ControlGroup;
constructor(public fb:FormBuilder) {
this.myForm = fb.group({
'city': ['']
});
this.city.valueChanges.subscribe((value:string)=> {
console.log(value)
});
}
}
select 用 FormBuilder 定义 一个Control , 触发valueChanges事件,事件写在constructor里面
console.log('1') 会调用2遍