0

For a given class I am declaring a few private variables in the constructor, and making some calls. I can getting a No Provider error on my typed array declaration when including it in the constructor, and no errors when declaring it outside the constructor.

This code works, error free

  private data: Array<MyType>;

  constructor(private service: Service) {
    this.service.getData().then(response => this.data = response.json());
  }

Then, paradoxically, the below does not work, and produces the error found below the snippet.

  constructor(private data: Array<MyType>, private service: Service) {
    this.service.getData().then(response => this.data = response.json());
  }

Error from console is

EXCEPTION: Error: Uncaught (in promise) ORIGINAL EXCEPTION: No provider for Array!

Is this some syntax error? Am I horribly misunderstanding a concept? Both implementations appear identical and feel like they should behave exactly the same.

1
  • I assume this is Angular? Commented Aug 4, 2016 at 16:04

1 Answer 1

1

The code is equivalent... except for the call to the constructor. ;)

In the first case:

new TheClass(service);

In the second case:

new TheClass(somethingHere, service);
Sign up to request clarification or add additional context in comments.

Comments

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.