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.
subscribe(fn, fn, fn)thisissues.