【问题标题】:Trigger valueChange with Initialized value - angular2使用初始化值触发 valueChange - angular2
【发布时间】:2017-04-30 21:21:43
【问题描述】:

我正在编写一个 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);
  }
}  

所以,myControlformControlprofilesBy 是数组的 Observable。
formatQuery 只是使用 select 的值格式化查询的正文,getGroupBy 返回 http 请求(即:http.post(url, body) ...)。
然后我分配响应: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>

当用户选择一个值时它工作得很好,它会触发 valueChanges 并链接操作!

所以我很高兴。我认为这是一种优雅的方式,而不是使用ngOnChanges()

现在在这里我找不到使它起作用的方法。基本上,我想用一个值初始化选择并触发(无需任何用户操作)valueChange

我尝试使用 [(ngModel)] :&lt;select [formControl]="myControl" [(ngModel)]="selectedGroupBy"&gt; 但它并没有触发它。

最后,我尝试在ngOnInit()方法中自己拨打电话

this.getGroupBy(new formatQuery(this.selectedGroupBy.value), this.url)
       .subscribe((response) => {
         this.profilesBy = response.json().aggregations.group_by_type.buckets;
       });

但我得到的是Array 而不是Observable of Array!我想念什么? 我怎样才能让它发挥作用?


使用startWith的可能解决方案

this.profilesBy = this.myControl.valueChanges
      .startWith(DEFAULT)
      .map(text => new formatQuery(text.value))
      .switchMap(body => this.getGroupBy(body, this.url), (_, res)=> res.json().aggregations.group_by_type.buckets);

【问题讨论】:

    标签: angular rxjs observable valuechangelistener ngoninit


    【解决方案1】:

    可能的解决方案是使用startWith 运算符:

    this.profilesBy = this.myControl.valueChanges
      .startWith(DEFAULT_OPTION)
      .map(text => new formatQuery(text.value))
      .switchMap(body => ...);
    

    【讨论】:

    • 但是startWith被添加到从valueChanges触发的每个新的Observable中,如何只做一次?
    【解决方案2】:

    要以编程方式更改 formControl 的值,您只需调用 setValue() 方法:

    setValue(value: any, {onlySelf, emitEvent, emitModelToViewChange, emitViewToModelChange}?: {
        onlySelf?: boolean,
        emitEvent?: boolean,
        emitModelToViewChange?: boolean,
        emitViewToModelChange?: boolean
      }) : void
    

    将表单控件的值设置为value。

    所以对于您的情况:this.myControl.setValue(foo) 应该触发 valueChanges

    【讨论】:

    • 嗨@n00dl3,谢谢你的回答。我不能让它工作。构造函数(公共 http: Http){ this.profilesBy = this.myControl.valueChanges //.startWith(this.groupBy[0]) .map(text => new formatQuery(text.value)) .switchMap(body => this .getGroupBy(body, this.url), (_, res)=> res.json().aggregations.group_by_type.buckets); } ngOnInit() { this.myControl.setValue(this.groupBy[0]); }
    • 好吧,我无法编辑我之前的评论...你知道为什么它不起作用吗?
    • 这应该是答案
    猜你喜欢
    • 2018-05-11
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多