I have a service that provides getter and setter methods, returning id: number
and title: String
values from my dialog component. I am trying to save these responses into my data
array but struggling to achieve it.
For instance:
0: {id: 0, title: "UK",…}
1: {id: 1, title: "Usd",…}
2: {id: 2, title: "ff",…}
3: {id: 3, title: "yy",…}
4: {id: 4, title: "nn",…}
5: {id: 5, title: "mh",…}
6: {id: 6, title: "tr",…}
7: {id: 7, title: "es",…}
I would greatly appreciate any assistance in resolving this issue.
This is what I currently have:
app.component.ts
export class AppComponent {
clickEventSubscription: Subscription
ngOnInit() {
}
id: number;
title: String;
data: any = [];
constructor(private share: ShareDataService) {
this.clickEventSubscription = this.share.getClickEvent().subscribe(() => {
this.initialize();
})
}
initialize() {
this.id = this.share.getId();
this.title = this.share.getTitle();
console.log(this.id, this.title);
}
}
app.component.html
<app-dialog></app-dialog>
<h2>Add values of my service into array:</h2>
<button (click)="initialize()"></button>
share-data.service.ts
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ShareDataService {
title: String;
id: number;
getId() {
return this.id
}
getTitle() {
return this.title;
}
private subject = new Subject<any>();
sendClickEvent() {
this.subject.next();
}
getClickEvent(): Observable<any> {
return this.subject.asObservable();
}
}
Thank you very much!