Throughout my journey building my Angular 2 website, I've found the Elvis Operator
to be a crucial element that makes everything possible. It seems like every task I undertake involves mastering how to apply it correctly in every instance where data is involved. If needing a ?
on almost everything you do with real data in regular life was normal, I'm sure the Angular documentation would mention it.
For example, when setting up FormGroups
for reactive forms following the steps on Angular.io, my form group initially looked like this:
createForm() {
this.quesForm = this.fbuild.group({
question: this.featureQuestion.question,
id : this.featureQuestion.id,
name : this.featureQuestion.name,
answers : this.fbuild.array([])
});
this.setAnswers(this.featureQuestion.answers);
}
get answers(): FormArray {
return this.quesForm.get('answers') as FormArray;
};
Using the mock-data
I created in a const
, everything worked fine. But when transitioning to use "real world data," I had to adjust the code as follows after spending several days figuring out the new requirements:
createForm() {
this.quesForm = this.fbuild.group({
question: this.featureQuestion ? this.featureQuestion.question: '',
id : this.featureQuestion ? this.featureQuestion.id: '',
name : this.featureQuestion ? this.featureQuestion.name: '',
answers : this.fbuild.array([])
});
this.setAnswers(this.featureQuestion ? this.featureQuestion.answers: []);
}
It doesn't matter if I'm binding to HTML, attributes, using @Input
and @Output
, or working within functions; the Elvis Operator
always presents itself as the challenge that requires time and effort to solve, even for tasks I thought I already understood. Initially, I believed it might just be a minor issue to resolve, but now I realize it's a significant aspect personally affecting me, suggesting it may not solely be a bug on their end since there isn't more extensive information available. This implies that nobody could effectively use Angular in real-life scenarios without consistently applying this operator throughout the development process, hinting at something I might be overlooking.
Here's an illustration of how I fetch data from Firebase:
Service
export class HomeService {
BusImage : FirebaseObjectObservable<any>;
constructor(private af: AngularFire) {}
getBusImage() {
this.BusImage = this.af.database.object('/page_area_business_image')
as FirebaseObjectObservable<any>
return this.BusImage;
}
}
Parent Component
export class ExpBusinessComponent implements OnInit {
private busImageO : FirebaseObjectObservable<any>;
private busImageQues01O : FirebaseObjectObservable<any>;
private busImageQues02O : FirebaseObjectObservable<any>;
constructor(private _homeService: HomeService) {}
ngOnInit() {
this._homeService.getBusImage().subscribe(BusImage => {
this.busImageO = BusImage.expand;
this.busImageQues01O = BusImage.questions.question01;
this.busImageQues02O = BusImage.questions.question02;
});
}
}
Parent Template
<h1>{{busExpInformation?.title}}</h1>
<p>{{busExpInformation?.intro}}</p>
<business-image
[busImageI] = "busImageO"
[busImageQues01I] = "busImageQues01O"
[busImageQues02I] = "busImageQues02O">
</business-image>
Child Component
...The rest of the provided code has been omitted for brevity. In essence, I've encountered various challenges while working with Angular and applying the Elvis Operator
correctly with my data remains crucial for successful outcomes. Despite facing setbacks and unexpected fixes due to database interaction, I strive to align with Angular best practices and suggestions to streamline the development process.