AngularJS: The right place for a global service provider, service, or rootScope?

I am new to AngularJS, and - since it is a rather complex and new approach for me, I am a bit confused.

I come from a "classic" background (server-side template languages ​​[for example, Yii, django, Smarty] + some jQuery to make things a bit dynamic). A.

Let's say I have a menu bar (Bootstrap NavBar or something else) - an element that lives outside the contents of the main page, for example:

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

Now I would like to make the menu a bit dynamic, that is, add or remove some menu items inside the controller. Using frameworks on the server side and their template system, for example, Yii, I would just have a BaseController class with the $menuItems variable and display it in the menuBar each time, while all controllers would inherit from BaseController so that they could change the elements.

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

What is the Angular way for something like this? So far, I have been thinking of creating a custom service or the $rootScope .

+8
javascript angularjs angularjs-scope javascriptmvc
source share
1 answer

UPDATE . I would advise you to take a look at John Papa's ng-demoes and style guide as the second step in making angular.js.

Check out the recent angular.js team examples to show the full app.

Pay attention to the following functions angular gives you

I believe that this will be a good approach to combining the service and directives for the rendering menu the way you described it.

+9
source share

All Articles