Issue
So i have a service with the following code
@Injectable({ providedIn: 'root' })
export class FileService {
private localfiles: UploadedFile[] = [new UploadedFile('asdasd', 'sdfsdfsd', false, 1, 2, 'dsfsf')];
filesChanged = new Subject<UploadedFile[]>();
constructor() {}
setFiles(file: UploadedFile) {
this.localfiles.push(file)
console.log(this.localfiles)
this.filesChanged.next(this.localfiles.slice())
console.log('after')
}
I then have a component as follows
export class FileslistComponent implements OnInit {
files: UploadedFile[];
constructor(private fileService: FileService) { }
ngOnInit(): void {
this.files = this.fileService.getFiles();
this.fileService.filesChanged.subscribe((files: UploadedFile[])=>{
console.log('never gets here')
this.files = files;
})
}
}
When I call setFiles the array is updated as can bee seen from the console log of the array. But the console log inside the subscribe within the component is never display and the array there isn't updated.
Solution
Thanks for the help. It's amazing what a nights sleep will do. I'd started providing the service at component level. Later changed my mind and moved it to root, but forgot to stop providing it at component, and so as such had 2 different versions of the service.
Removed the provision at component level and all now works as expected.
Answered By - Pete
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.