1

I need to update a property in a child component when a push notification is received from firebase. Firebase is configured correctly and I have the property configured as an input properly. The value is updated in the child successfully when I manually trigger the change using a button. However, when the value is changed using a callback the value will be updated but the child is not notified of the change.

export class AppComponent implements OnInit {
    private orders_updated:boolean = false;

    ngOnInit(): void {
        //firebase.init() removed for space
        firebase.addOnMessageReceivedCallback((data) => {
            this.interpret_message(data);
        });
    }

    private clicked() {
        this.orders_updated = !this.orders_updated;
        console.log(this.orders_updated)
    }

    private interpret_message(message:any) {
        if (message.body == "A new order was submitted") {
            this.clicked();
        }
    }
}
export class TransactionComponent implements OnInit {
    private _update:boolean;

    @Input() set update(value: boolean){
        this._update = value;
        console.log("Value updated")
    }
}

When the clicked function is called manually I receive an update that the value in TransactionComponent was updated. However, when clicked is called as the result of a callback I do not receive "Value updated" message. I believe that by using an arrow function I am not running into a scope issue.

1
  • have you tried using ngOnChanges, as OnChanges hook is used to check when input changes Commented Aug 12, 2019 at 16:23

1 Answer 1

0

You are not guaranteed to be operating in the same NgZone when responding through an async channel like firebase.

export class AppComponent implements OnInit {
    private orders_updated:boolean = false;

    constructor(private zone: NgZone) {}

    ngOnInit(): void {
        //firebase.init() removed for space
        firebase.addOnMessageReceivedCallback((data) => {
            this.interpret_message(data);
        });
    }

    private clicked() {
        this.zone.run(() => {
          this.orders_updated = !this.orders_updated;
          console.log(this.orders_updated)
        });
    }

    private interpret_message(message:any) {
        if (message.body == "A new order was submitted") {
            this.clicked();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.