简体   繁体   English

子调用具有http请求的父函数后,如何在子组件中更新父变量

[英]How to update parent's variable in child component after child has called parents function that has http request

I cannot seem to see the changed @Input in the child component after it was updated in parents http request. 在父级HTTP请求中更新子组件后,我似乎看不到已更改的@Input。

This is an example: 这是一个例子:

Parent 父级

import { ChildComp } from './child.component';

@Component({
    template: <child [counter]="a" [updateCounter]="updateCounter"></child>,
    directives: [ChildComp]
})

export class ParentComponent { 
    public a = 1;
    public CallBack:Function;

    constructor(private _http:Http) {}

    public ngOnInit() {
        this.CallBack = this.updateCounter.bind(this);
    } 

    public updateCounter() {
        this._http.get('/newcounter/').subscribe(
            data => {
                // update counter
                this.a = JSON.parse(data['_body']);
            },
            error => {},
            () => console.log('updated counter')
        );

    }
}

Child 儿童

@Component({
    selector: 'child',
    template: `
        <button class="btn btn-default" (click)="updateCounter()"></button>
        <p>counter: {{ counter }}</p>
    `,
    inputs: ["counter", "updateCounter"]
})

export class ChildComponent { 
    public counter;
    public updateCounter:Function;

    constructor() {}
}

So, this works if there is not http request. 因此,如果没有http请求,这将起作用。 But once I have the request, the child view will not update the counter . 但是,一旦收到请求,子视图将不会更新计数器

Any ideas? 有任何想法吗? What am I missing? 我想念什么?

One hack i have now is to setTimeout on child component to update the counter 500ms after the call to updateCounter 我现在有一个办法是在调用updateCounter之后500ms上对子组件设置setTimeout来更新计数器

Amend the updateCounter function of your parent component like this: 修改父组件的updateCounter函数,如下所示:

public updateCounter() {
    let that = this;
    this._http.get('/newcounter/').subscribe(
        data => {
            // update counter
            that.a = JSON.parse(data['_body']);
        },
        error => {},
        () => console.log('updated counter')
    );

}

Using this in the promise does not reference your class anymore. 使用this在承诺不引用你的类了。 So you need to keep a reference to this in another variable and use that one instead. 因此,您需要在另一个变量中保留this的引用,并改用该变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当子组件嵌套了多个父组件时,从子组件调用超级父函数 - Calling to super parent function from child component when it has nested multiple parents 从子组件调用的反应父函数不更新状态? - React Parent Function Called from Child Component does not Update State? 从父组件 React JS 调用 map function 时如何更新子组件的 state - How to update state of child component when it get called in map function from parent component React JS 加载父组件数据后,如何从子组件调用函数(Angular 5) - How to call function from child component when parent component data has loaded (Angular 5) 子组件更新服务器后如何更新父组件的state? - How to update state of parent component after the server is updated in child component? 如何从Reactjs中的子组件更新父组件的状态 - How to update a parent's component state from a child component in Reactjs 父组件的 fetch 调用完成后如何提醒子组件? ReactJS - How to alert child once the parent component's fetch call has been completed? ReactJS React Native-子事件调用后,子组件将无法完成渲染,从而更新了父状态 - React Native - Child Component won't finish rendering after Child Event called that updates Parent's state 反应从父组件更新孩子的状态 - React Update Child's state from Parents Component 子组件调用父组件的功能 - Child component call parent's component function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM