Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to call Loopback4 controller's method from another controller? #3028

Closed
dinsvale opened this issue Jun 1, 2019 · 5 comments
Closed

How to call Loopback4 controller's method from another controller? #3028

dinsvale opened this issue Jun 1, 2019 · 5 comments

Comments

@dinsvale
Copy link

dinsvale commented Jun 1, 2019

Description / Steps to reproduce / Feature proposal

I have a loopback 4 controller with a function that I don't want to expose via HTTP.
I would like to be able to call the function from another controller. How can I do this? Is there any way of injecting a controller in another controller? (I 'm able to inject repositories in controllers, but not controllers in controllers)

Current Behavior

Expected Behavior

See Reporting Issues for more tips on writing good issues

@raymondfeng
Copy link
Contributor

You should be able to inject another controller. Pls show your code.

@raymondfeng
Copy link
Contributor

@raymondfeng
Copy link
Contributor

It should be as simple as @inject(‘controllers.AnotherController’) c: AnotherController

@dinsvale
Copy link
Author

dinsvale commented Jun 1, 2019

Alright, so to inject a repository in a controller, I do this, in the constructor of the controller:
constructor( @repository(RepositoryIWantToInject) public repositoryIWantToInject: RepositoryIWantToInject, ) {}

I tried @controller but it didn't work. But what you posted just now seems to have worked! Thanks!

@raymondfeng
Copy link
Contributor

Simply use @inject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants