0

I have a user.list.ctrl and a user.detail.cntr. All the controllers are build as a module and are injected in a "user-module" which I inject in the app.js. (see the complete code in the plunker below)

my controller module

angular.module('user-module', ['user-module.controllers']);

my user-module

angular.module('demo.app', ['user-module']);

In both controllers i inject user-Fctr with data from a REST factory. (works well)

user.list.cntrl has a $scope.refresh() user.detail.cntrl has a $scope.update()

user.list.cntrl

    1. When I enter a new record, i call the $scope.refresh() so I can refresh the list. (this is working fine)

user.detail.cntrl

    1. When i click a user from the list, the user detail loads in a different view (works ok)
    1. when I update the user.detail, I want to call $scope.refresh() to update the user.list , but it is not working. I cannot call $scope.refresh()

I thought that since I inject the same factory into both controllers I can use each others $scopes.

Any ideas on how I can use $scope.refresh() (or update the list when I update the user.detail.js)

I make a plunker with all the js files (the plunker is not functional, it is only to show the code that I have) http://plnkr.co/edit/HtnZiMag0VYCo27F5xqb?p=preview

thanx for taking a look at this

2 Answers 2

1

This is a very conceptual problem.

You have created a controller for each "piece" of view because they are meant for different activities. This is the purpose of controllers. So that is right.

However, you are trying to access the refresh function, written in one controller, in another one. Taken literally, this is wrong, since then, refresh is out of place either inside the user list controller or the detail controller.

A function that is meant to control (literally) what is happening on a specific piece of view is a controller. - There you are right having a controller for the list and one for the details.

A function that is meant to be shared between controllers must be a service. This is exactly what you want for your refresh function to be.

Whenever you inject the same factory into n controllers, you can't use the scope of every controller. This isn't the purpose of a controller.

However, whenever you inject the same factory into n controllers, you can use its exposed methods.

The problem you have, can be solved as follows:

app.factory( 'sharedFunctions', [ 'factoryId', function sharedFunctions( factoryId ) { 
  var refresh = function () {
   factoryId.getAll(/*your params to query*/)
     .success( function ( response ) {
        //This will return the list of all your records
        return response;
     }); 
  };
  return sharedFunctions;
}]);

With this factory service registered, then you can inject it to your controllers and whenever you need to refresh, just call the exposed method of the service and plot the new information into the view.

Hope it works for you!

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

2 Comments

i see what you're saying. I makes sense. I'll try and I'll post back. thank you
i tried it, how do I call the "exposed method" of the service? can you please clarify. thanx again
0

i ended up doing this:

I added in the list.contrl this:

 factoryId.listScope = $scope; 

since I already have the factoryId (my data service) injected in the detail controller, I can call this:

factoryId.listScope.refresh();

it works but I don't know if this is the best way. any 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.