title: How do I pass data from one component to another in Angular?
body: <p>I am working on a quiz app with Angular. I need to get the value of correctAnswersCount in my ResultsComponent. I'm getting the value of 0 for correctAnswersCount in ResultsComponent when I really need the value of the total correct answers at the end of the quiz, and also getting value of 1166 during the quiz in place of correctAnswersCount. I think the code looks correct, just the value that I'm getting is wrong. Not sure what I'm doing wrong. This is my code:</p>
  
  <p>in my QuizService:</p>
  
  <pre><code>    @Injectable({ providedIn: 'root' })
      export class QuizService {
        correctAnswersCountSubject = new BehaviorSubject&lt;number&gt;(0);
  
        sendCountToResults(value) {
          this.correctAnswersCountSubject.next(value);
        }
      }
  </code></pre>
  
  <p>in my DI-Quiz Component:</p>
  
  <pre><code>    ngOnInit() {
        this.quizService.correctAnswersCountSubject.subscribe(data =&gt; {
          this.count = data + 1;
        });
        this.sendCountToQuizService(this.count); 
      }
  
      sendCountToQuizService(count) {
        this.quizService.sendCountToResults(count);
      }
  </code></pre>
  
  <p>and in my ResultsComponent:</p>
  
  <pre><code>    constructor(private quizService: QuizService) {}
  
      ngOnInit() {
        this.quizService.correctAnswersCountSubject.subscribe(data =&gt; {
          this.correctAnswersCount = data;
        });
      }
  </code></pre>
  
  <p>Please can you help. Thank you.</p>