0

I have a simple component structure where I have an angular component called gameplay, inside which is a child component called question-box. The objective is to fetch a simple string via a backend service in gameplay (which works fine), and pass this string to the question-box to be displayed (which doesn't work).

Here is my code:

gameplay.component.ts:

import { Component, OnInit, OnChanges } from '@angular/core';
import { QuestionsService } from '../services/questions.service';

@Component({
  selector: 'app-gameplay',
  templateUrl: './gameplay.component.html',
  styleUrls: ['./gameplay.component.css'],
})
export class GameplayComponent implements OnInit {

  constructor(private qService: QuestionsService) {

  }

  currentQuestion: string;

  ngOnInit() {
    this.qService.getQuestions().subscribe({
      next(data) {
        this.currentQuestion = data[0].question;
        console.log(this.currentQuestion); //logs correctly
      },
      complete() { console.log('Done')},
      error() { console.log('Error')},

    })
  }
}

gameplay.component.html:

<app-question-box [question]="currentQuestion"></app-question-box>

question-box.component.ts

 import { Component, OnInit, Input, OnChanges, SimpleChange } from '@angular/core';

@Component({
  selector: 'app-question-box',
  templateUrl: './question-box.component.html',
  styleUrls: ['./question-box.component.css']
})
export class QuestionBoxComponent implements OnInit {

  @Input() question: string;

  constructor() {
  }

  ngOnInit() {
  }
}

question-box.component.html

<mat-card md-colors="{background: 'orange'}">
  <mat-card-content>
    <p>{{question}} </p>
  </mat-card-content>

</mat-card>

I tried using the async pipe in the template, but it didn't work. I expect to see the string I got in next() in the child's <p>; but I don't see anything. The string gets logged correctly on the console, however.

Any help would be appreciated. Please let me know if I left out some information needed to find a solution. Thanks.

7
  • you need to remove the brackets juste after subscribe : subscribe(fn, fn, fn) Commented Oct 2, 2018 at 8:42
  • Thanks. Gonna try that Commented Oct 2, 2018 at 8:43
  • @Cétia: Looks like that's just a syntax difference. angular.io/guide/observables. Anyway, like I said, the next method seems to work because I see the console log correctly. Commented Oct 2, 2018 at 8:50
  • do you use Http or HttpClient service? Commented Oct 2, 2018 at 9:38
  • 1
    @Marcel: Thanks it worked. If you or Celia post it as an answer I'd be happy to accept. Lesson learned. When in doubt, use arrow functions. No this issues. Commented Oct 2, 2018 at 10:06

2 Answers 2

1

According to my comment:

If you pass the callbacks this way, the this inside the callbacks is the function, not the component itself.

Changing to arrow functions will solve the problem:

this.qService.getQuestions().subscribe(
      data => this.currentQuestion = data[0].question,
      error => console.log('Error'),
      () => console.log('Done')
);
Sign up to request clarification or add additional context in comments.

Comments

0

Can you add an ngOnChanges to the child component to check if the change has been propagated... something like this:

ngOnInit(change: any) {
    console.log(change['question']);
}

Also, try changing the ngOnInit in the parent to be like this:

ngOnInit() {
    this.qService.getQuestions().subscribe(
      data => {
         this.currentQuestion = data[0].question;
         console.log(this.currentQuestion); //logs correctly
      },
      error =>  console.log('Error')

    );
}

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.