As of late, I have delved into learning Angular but encountered an issue today. I have the following Component:
import { Component, OnInit } from '@angular/core';
import { SharedService } from '../home/shared.service';
import { IData } from '../data/IData';
@Component({
selector: 'app-current-article',
templateUrl: './current-article.component.html',
styleUrls: ['./current-article.component.css']
})
export class CurrentArticleComponent implements OnInit {
data: IData;
symbols: number = 150;
showReadMore = true;
contentToShow: string = "";
constructor(private _sharedService: SharedService) { }
ngOnInit() {
this.data = this._sharedService.sharedData;
this.contentToShow = this.data.content;
}
readMore() {
//this.symbols += 100;
}
}
The "data" property comprises the properties: title (string), authorId (number), and content (string).
Additionally, I implemented the solution from this post and created this service:
import { Injectable } from '@angular/core';
import { IData } from '../data/IData';
@Injectable()
export class SharedService{
sharedData: IData = {
title: "",
authorID: 0,
content: ""
};
insertData(data: IData){
this.sharedData.title = data.title;
this.sharedData.authorID = data.authorID;
this.sharedData.content = data.content;
}
}
Furthermore, I have a view for the Component:
<div *ngIf="data.title">
<h1>{{ data.title }}</h1>
<h5>Author ID: {{ data.authorID }}</h5>
<div>
<span>{{ contentToShow }} </span>
<span>
<a href="#" *ngIf="showReadMore" (click)="readMore()">Read More ↪</a>
</span>
</div>
</div>
However, I am facing an issue with the "contentToShow" property appearing as undefined, which is puzzling to me. Strangely, when I use "data.content" instead, it displays the data perfectly. Any insights into why this might be happening? I have been trying to solve it for the past 2 hours, and it seems like it might be something blatantly obvious that I'm missing. Appreciate any help! Thank you!