Linh
Linh

Reputation: 1022

How to call a method of a controller which belongs to another view in AngularJS?

In indexController.js of index.html, I want to call a method of loginController.js which does not belong to index.html. I already tried the following way: Firstly, I imported the controller in index.html by using

<script src="loginController.js"></script>

After that, I used $emit to call the method of loginController.js but I was failed. I suppose that loginController.js is not initialized in index.html, it does not belong to the module of index.html as well so it is not available for indexController.

I hope to be received some useful suggestion so I can resolve this problem.

Upvotes: 0

Views: 28

Answers (1)

z.a.
z.a.

Reputation: 2777

You should use services instead in between controllers. Every controller has its own function scope, so unless they are nested, even then it's not a good practice, it won't work.

Create a loginService with the methods you need, then use it anywhere you want.

Upvotes: 1

Related Questions