In an angular app, I'm trying to cache HTTP results into a local variable by assigning the JobServicejobs:Job[] variable to the result of the request. I then wish to filter the results invoking the setSearch() function from another component that injects the JobService.
When the UserService retrieves the user, the JobService subscribes to the event and gets the jobs. This means that if the user state changes so does the jobs array. This then invokes setJobs() to set the local jobs variable
Later when a user types in the search box, setSearch() is called and this.jobs is an empty array even though it was set in setJobs()
The jobsPublisher then publishes this new set of jobs to the subscribers.
Note: HttpCacheService is very similar to Http but it just maps the response to the object desired, whilst handling and logging errors.
Summary
In an angular app, I'm trying to cache HTTP results into a local variable by assigning the
JobService
jobs:Job[]
variable to the result of the request. I then wish to filter the results invoking thesetSearch()
function from another component that injects theJobService
.When the
UserService
retrieves the user, theJobService
subscribes to the event and gets the jobs. This means that if the user state changes so does the jobs array. This then invokessetJobs()
to set the localjobs
variableLater when a user types in the search box,
setSearch()
is called andthis.jobs
is an empty array even though it was set insetJobs()
The
jobsPublisher
then publishes this new set of jobs to the subscribers.Note:
HttpCacheService
is very similar toHttp
but it just maps the response to the object desired, whilst handling and logging errors.Code
Demos
I've created a working example and a broken example.
Jobs becomes empty after assignment Variable is assigned
Environment
Browser: Chrome (desktop) version XX
For Tooling issues: