Within the past few days, I delved into Angular and decided that creating a simple single page application would be a great starting project. This endeavor aims to pave the way for a much larger project in the future.
I find myself grappling with understanding how pages (url locations) align with modules (reusable blocks with related controllers) on the page.
My initial approach was to define pages using a routing block like so:
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function($routeProvider) {
$routeProvider.
when('/home', {
templateUrl: 'home.html'
}).
when('/page2', {
templateUrl: 'page2.html'
}).
otherwise({
redirectTo: '/home'
});
});
This routing block would reside in its own config.js file.
In addition, I envisioned having controllers tied to partials that could be reused across different pages.
For instance, let's consider a menu module that should appear on both the home and page2 pages.
I wanted this setup in a standalone file, similar to the following:
<div ng-controller="navMenu">
<ul>
<li>...
</ul>
</div>
This structure allows me to organize my pages - such as home, page2, and others - in the following manner:
<ng-include src="/app/modules/menu.html"></ng-include>
<ng-include src="/app/modules/somethingElse.html"></ng-include>
<ng-include src="/app/modules/anotherThing.html"></ng-include>
Furthermore, my index.html body would look like this:
<ng-view></ng-view>
However, a roadblock emerged during implementation - the inability to dynamically load controllers. As a result, I would have to list out all possible controllers in one large file, contradicting one of Angular's key benefits.
Therefore, it appears that I may not have fully grasped Angular's concepts, prompting me to seek guidance. My goal is to have small reusable HTML blocks, each with its distinct controller, which can be loaded dynamically.
Many tutorials suggest assigning a controller to each page in the routing config, but given the shared functionality between pages, this approach seems redundant. Any assistance, resources, or ideas would be greatly appreciated. Thank you.