0

I am very new to angular and JS and do not know how to implement very simple behavior. So... I have an angular-route module and all my views are changed with ng-view directive. Here is the declaration

<body>
    <div class="row main" ng-view> </div> 
<body>

I have all controllers connected to those views. Also I have one common controller to all views, I check cookie in that controller. How to call that controller automatically in all other views without dependency on that controller from others?

I tried to put it in but it is called only once on the index page.

Dairo
  • 822
  • 1
  • 9
  • 22
yu.pitomets
  • 1,660
  • 2
  • 17
  • 44
  • Check this out, http://stackoverflow.com/questions/22480738/angular-ui-router-call-parent-controller-function-from-child-controller – graphefruit Sep 25 '14 at 19:09
  • 1
    the question for me would be is it really a controller that you need. As i understand you do want to execute some code (for cookie checking) for every page, what is probably not needed. If you want to check if the user has a cookie i would recommend doing this in the app code. – AirBorne04 Sep 25 '14 at 19:12
  • You mean in server side? – yu.pitomets Sep 25 '14 at 19:15

2 Answers2

0

You can do something like this:

<body>
    <div ng-controller="globalController">
        <div class="row main" ng-view> </div> 
    </div>
<body>

http://plnkr.co/edit/k2H8BsTCVh24nbR07GjP?p=preview

Marian Ban
  • 8,158
  • 1
  • 32
  • 45
0

A controller, by definition, is meant to prepare the data to be passed to the view.

I would recommend you to do this with a service instead.

This service could then be accessed by whatever view you are in.

You could imagine, for example, to implement a resolver in you $routeProvider.when which will read the Cookie from this service. $route also trigger events whenever the view is changing, will change or has changed. Try to leverage this, this is really powerful.

DevLounge
  • 8,313
  • 3
  • 31
  • 44