3

As a beginner in angular, I am running across an issue whilst trying to build my app via angular-cli.

After following this tutorial:

My appModule looks like:

...
providers: [
{ provide: HttpService, useFactory: (backend: XHRBackend, options:     RequestOptions) => {
return new HttpService(backend, options); },
  deps: [XHRBackend, RequestOptions]}
]
...

When building it I get:

Error: Error encountered resolving symbol values statically. Function calls are
not supported. Consider replacing the function or lambda with a reference to an
exported function (position 63:39 in the original .ts file), resolving symbol Ap
pModule in ../angular/app/src/app/app.module.ts
at positionalError (..\angular\app\node_modules\@angular\compiler-c
li\src\static_reflector.js:595:18)

When I use ng serve the app works without any problems.

3 Answers 3

6

Try this:

export function httpServiceFactory(backend: XHRBackend, options: RequestOptions) {
  return new HttpService(backend, options);
}  
  
providers: [
  { 
    provide: HttpService, 
    useFactory: httpServiceFactory,
    deps: [XHRBackend, RequestOptions]
  }
]

When you're building it's trying to do AoT compilation which fails when encountering the lambda function, so you need to export it. While when using ng-serve it's using JiT compilation.

See: https://github.com/angular/angular/issues/11262#issuecomment-244266848

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

Comments

1

I've seen this before, and not really sure what causes this error (i.e. why it works in some cases and others it doesn't), but you can resolve it by doing what the error says: "Consider replacing the function or lambda with a reference to an exported function".

export function httpFactory(backend: XHRBackend, options: RequestOptions) {
  return new HttpService(backend, options);
}

...

providers: [
  { 
    provide: HttpService,
    useFactory: httpFactory,
    deps: [XHRBackend, RequestOptions]
  }
]

Comments

1

Instead of an inline function, use an explicit function:

function httpFactory(backend: XHRBackend, options: RequestOptions) {
  return new HttpService(backend, options);
}
...
providers: [{ provide: HttpService, useFactory: httpFactory,
                deps: [XHRBackend, RequestOptions]}
]
...

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.