2

I am trying to get my data service to accommodate route parameters.

  @Injectable({
    providedIn: 'root'
  })
  export class DataService implements InMemoryDbService {
  
  createDb(): {
    products: IProducts[],
    product: IProduct
  } {
    const products = ProductsData.products;
    const product = ProductsData.product;

    return { products, product };
  }
}

When I'm executing a route like products/123 I want the system to get the product collection. I've tried different approaches using the environment file and the product collection within the products-data.ts file, but I'm not having any luck. Are there examples available? For that matter, how should the data service be configured to accommodate query strings?

1 Answer 1

1

The property of the object you want to filter will be the key of the query string.

api/heroes/?name=${term}

Here we filter name of the object { id: 0, name: 'Zero' }.

Just search for "tour of heroes stackblitz" to get wide range of examples for you to use as reference.

Forked Stackblitz

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I have succeeded in filtering based on a route parameter that matches a record ID. Two questions remain: 1) can a record ID be itemId or does if have to be just id, and 2) can query parameters be used to filter on a range, like all the records between a given from and to date?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.