首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度2可观测刷新

角度2可观测刷新
EN

Stack Overflow用户
提问于 2017-02-15 14:55:13
回答 1查看 4.6K关注 0票数 2

在从子组件进行更新操作后,我正在尝试刷新父级的表。

@服务

代码语言:javascript
复制
getLicenses(){
     ...
     return this.http.post(url","",options)
        .map((result: Response) => result.json())
          .map((data: License[]) => {
              this.licenses = data;

              return this.licenses;
            })
            .catch(this.handleError);
}

@父组件

代码语言:javascript
复制
 ngOnInit() {
    this.licenseService.getLicenses().subscribe(licenses => {
                                          this.licenses = licenses;
 }); 

一切看起来都很好的初始化。我桌子上有数据。当从表中选择行时,我将其传递给子组件并执行更新操作。更新后,我希望刷新表并调用服务的getLicenses()方法。但是它不是refresh.If --我调用另一种方法来测试,它刷新了我的表。

代码语言:javascript
复制
  getLicenses2(){
    this.licenses.pop();
    this.licenses.pop();
    this.licenses[2].isFree=  !this.licenses[2].isFree;
  }

此外,我还测试了如下所示,这不会刷新。

代码语言:javascript
复制
  getLicenses2(){

       this.http.post(url,"",options)
            .map((result: Response) => result.json())
              .map((data: License[]) => {
                  this.licenses = data;
                  this.licenses.pop();
                  this.licenses.pop();
                })
                .catch(this.handleError);
  }

当我手动更改数组时,为什么要这样做呢?当我分配json的数据时,为什么不工作?

我的响应是好的,没有例外,第一次初始化工作,well.Update操作也是成功的,如果我只是用f5刷新表,新的数据即将到来。(我对角质2很陌生)

编辑

如果我只是改变数组,但当我在http post中这样做的时候,它就不能工作了。

我试过了。这一次没有孩子。当我调用http post时初始化不工作。见下面的测试方法。

@服务

代码语言:javascript
复制
private licenses2 = new Subject<License[]>();
licenses2$ = this.licenses2.asObservable();

@父组件

代码语言:javascript
复制
 ngOnInit() {
    this.licenseService.licenses2$.subscribe(licenses => {
        this.licenses = licenses;
     } );
    this.licenseService.getLicenses2();
 }

作品

代码语言:javascript
复制
getLicenses2() {
   let test: License[] = [];
   test.push(new License());

   this.licenses2.next(test)
}

不工作

代码语言:javascript
复制
 getLicenses2(){
   this.http.post(URL,"",options)
        .map(response => {
              this.licenses2.next(response.json() as License[])
        })
        .catch(this.handleError);
  }

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-15 20:43:32

一种解决方案是使用Subject。根据我从您的评论中了解到的情况,您正在尝试从子表中更新父表,这是行不通的,因为只有在您这样做的情况下,更新才会出现在子表中。因此,您可以做的是,告诉家长更新数据时,您需要这样做。因此,请尝试以下几点:

在父母中声明一个主题:

代码语言:javascript
复制
public static updateStuff: Subject<any> = new Subject();

并且在父构造函数中订阅:

代码语言:javascript
复制
ParentComponent.updateStuff.subscribe(res => {
    // here fire functions that fetch the data from the api
    this.getLicenses(); // add this!
})

在您的子组件中,每当您在父组件中进行应该更新的更改时,请执行以下操作:

代码语言:javascript
复制
ParentComponent.updateStuff.next(false);

例如,在您的孩子做完帖子请求后:

代码语言:javascript
复制
// this is the method you call in your child component to do the post request
postChanges() { 
  this.myService.updateTable()
     .subscribe(data => {
        this.data = data;
        ParentComponent.updateStuff.next(false); // add this, will update the parent!
  })
}

..。然后它应该工作:) Remeber也取消订阅:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42252759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档