小编ByJ*_*yJC的帖子

触发valueChange with Initialized value - angular2

我正在写一个angular2应用程序而且我遇到了什么.首先,我有一个绑定到的选择formControl:

export class MyComponent implements OnInit {

  profilesBy: Observable<any[]>;
  myControl = new FormControl();

  constructor(public http: Http) {
    this.profilesBy = this.myControl.valueChanges
      .map(text => new formatQuery(text.value))
      .switchMap(body => this.getGroupBy(body, this.url), (_, res)=> res.json().aggregations.group_by_type.buckets);
  }
}  
Run Code Online (Sandbox Code Playgroud)

所以,myControlformControl,并且profilesBy是一个Observable数组.
formatQuery使用选择的值只格式化机构查询并getGroupBy返回一个http请求(即:http.post(URL,身体)...).
然后我分配了回复:res.json().aggregations.group_by_type.buckets
但是不要过多考虑这个问题.

这是我的模板:

<md-card>
    <h4>Profiles group by :
        <select [formControl]="myControl">
            Some options ...
        </select>
    </h4>

    <div *ngFor="let profile of profilesBy | async">
        <strong>{{profile.key}} :</strong> {{profile.doc_count | number:'1.0-2'}}
    </div>
</md-card>
Run Code Online (Sandbox Code Playgroud)

当用户选择一个值时它会正常工作,它会触发valueChange并链接动作! …

valuechangelistener observable rxjs ngoninit angular

4
推荐指数
2
解决办法
5350
查看次数