AngularJS: Correct place for global menu provider, service or rootScope?
Asked Answered
T

1

8

I'm new to AngularJS, and - since it is quite complex and the approach is new for me, I'm a bit confused.

I'm from "classic" background (server-side template languages [like Yii, django, Smarty] + some jQuery to make things a bit dynamic).

Let's say I have a menu bar (Bootstrap NavBar, or anything else) - an element which lives outside of the main page content, like this:

<body>
  <div id="menubar">
    ... <!-- menu content -->
  </div>
  <div class="container">
    <div ng-view></div>
  </div>
</body>

Now I'd like to make the menu a bit dynamic, i.e add or remove some menu items inside of the controller. Using the server side frameworks & their templating systems, for example Yii - I'd simply have a BaseController class with $menuItems variable, and render it each time in the menuBar, while all the controllers would inherit from BaseController so they could modify items.

Also, I need a function which handles the searchForm located inside menu bar. Where should it live?

What is Angular way for something like this? So far I've been thinking of creating custom service or extending $rootScope.

Tevere answered 3/6, 2013 at 7:47 Comment(3)
Have you seen example app ? github.com/IgorMinar/foodmeCustomer
@Customer thank you, I didn't knew this one. I wasn't aware I can use ng-controller along with ng-view :) Combining it with some shared service should do a trick. Make it a reply instead of comment and I'll mark it as the answer.Tevere
possible duplicate of global communication in angular module: event bus or mediator pattern/serviceTurbellarian
C
9

UPDATE: I would encourage you to take a look at John Papa's ng-demoes and style-guide as say second step in adopting angular.js.

Check out examples angular.js team posted recently, to show full application.

Pay attention to the following features angular gives you

I belive it is going be a good approach to have combination of a service and a directive for rendering menu the way you described it.

Customer answered 3/6, 2013 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.