0

Can you please tell me why my value in the html does not change when my variable gets updated? This is my code in the ts

private peer: any;
private anotherId: 'opopo'
public myPeerId: '123456';
ngOnInit(): void {
    this.peer = new Peer();

    this.peer.on('open', function (connectionId) {
      this.myPeerId = connectionId;
      console.log(this.myPeerId)
    });
  }

I can log my variable value when it changes but somehow, my html does not. Please see my html code

<h1>My id - {{myPeerId}}</h1>
<input type="text" [(ngModel)]="anotherId" >
<button (click)="connect()">Connect</button>

Thank you.

1 Answer 1

1

If you look at the console, you will see an error similar to

Cannot read property 'myPeerId' of undefined

To refer to the class scope using this keyword in callbacks, use arrow function notation

this.peer.on('open', (connectionId) => {     // <-- arrow notation here
  this.myPeerId = connectionId;
  console.log(this.myPeerId)
});
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.