kittencup / angular2-ama-cn

angular2 随便问
691 stars 101 forks source link

select 用 FormBuilder 定义 一个Control , 触发valueChanges事件 #77

Open chenyukunJoshHub opened 8 years ago

chenyukunJoshHub commented 8 years ago

select 用 FormBuilder 定义 一个Control , 触发valueChanges事件,事件写在constructor里面

this.modelForm.city.valueChanges.subscribe((value:string)=> {
           console.log('1')
})

console.log('1') 会调用2遍

kittencup commented 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)
        });

    }

}