Can I have a single route file in Angularjs and have multiple modules?

Asked

Viewed 753 times

0

I am interested in separating my app by module

app
controllers
views
diretivas
services
--modules
----principal
--------controllers
--------views
--------service
--------index.html
----cadastro
--------controllers
--------views
--------service
--------index.html
----financeiro
--------controllers
--------views
--------service
--------index.html
app.js

Being the first controller and views folder that would be the login and template of my system However all the examples I find, each module has its routes

would have as just a file manipulate all routes of all modules?

1 answer

1

Rod,

the user must be falling into index.html when entering the site, right? Just call the file routes inside it, example:

HTML

<html>
  <head>
    <script src="routes.js"></script>
  </head>
  <body>
    <div ng-view></div>
</html>

Routes.js

angularModule.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/principal', {
        templateUrl: 'principal/index.html',
        controller: 'PrincipalCtrl'
      }).
      when('/cadastro', {
        templateUrl: 'cadastro/index.html',
        controller: 'CadastroCtrl'
      }).
      when('/financeiro', {
        templateUrl: 'financeiro/index.html',
        controller: 'FinanceiroCtrl'
      }).
      otherwise({
        redirectTo: '/404'
      });
  }])

Of course you will have to adapt these routes to your need, it was just an example. But anyway, all the routes in a single file, that goes calling the respective controllers and their templates.

  • speaks rafael, so man, this way ceases to be modular, which is how I made my app. As for routes, because it’s an enterprise app, I don’t want you to keep track of all routes in a single file, and that, after logging in, the routes should be created according to your permission, coming from the Api, but thank you for responding

  • Rod, I understand the modular thing, but I confess I don’t fully understand what you need. I understood that the routes need to be in their respective modules, but you also want to create routes dynamically after login? And, what kind of manipulation is this that you need in a single file?

  • Rafael, then, in login, I do not want to expose all my routes, so I would redirect to a module, and after login create these routes dynamically, that would come from the return of the API

  • 1

    But why can’t these routes be created before login? If the user accesses a url that is not allowed for some reason, you redirect it. Create a Userservice for that. I figure every URL he accesses, you need to access the API to get his data, right? Will your API return saying that the user is not logged in? If so, this Userservice will do this access control. Got confused?

  • The problem, that being a corporate app, right on the login screen there will be all routes, then, anyone can see all my work modules, and even idea of what is my app, which who should know the news and etc is just my final customer. All access control is done via api yes, and is redirected if you do not have access

  • Just explain this part: "soon on the login screen there will be all routes". There will be how exactly?

  • rafael, if all the routes will be in the app.js file, just go to the source code of the page and you will have all the routes

  • Okay, I get it. I’m working on a solution.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.