I am developing an Angular SPA and I am currently in the design phase. I have created a sample architecture which I am planing to use in my SPA but I would get some feedback because I want the architecture to be scalable and maintainable. I have tested this architecture out and it works so far.
My Current Architecture:
- I have "Main" module where my
ngRoute
resides and from there I will control my apps routes. - I have created a separate module for each distinct functionality of my app for example (DashboardCtrl, AdminCtrl, blogCtrl etc)
- As its an SPA and its better to bootstrap only 1 angular app in the html page I bootstrap (
ng-app="mainApp"
) on the front-end. But I add all other modules within the main module as dependencies.
Important:
As its an SPA I am using resolve
property of ngRoute to get the data from the server before loading the view and then passing the resolved data to a specific controller responsible for that functionality.
Main Module:
var app = angular.module('app',['ngRoute','app.blog','routeService']);
app.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl: '/overview.html',
controller: 'mainCtrl'
})
// route for the about page
.when('/blog', {
templateUrl : '/blog.html',
controller : 'blogCtrl',
resolve: {
myDta: function(dataFetch){
return dataFetch.getProposals();
}}
})
// route for the contact page
.when('/bios', {
templateUrl : 'bios.html',
//controller : 'contactController'
});
});
app.run(['$rootScope', function($root) {
$root.$on('$routeChangeStart', function(e, curr, prev) {
if (curr.$$route && curr.$$route.resolve) {
// Show a loading message until promises are not resolved
$root.loadingView = true;
}
});
$root.$on('$routeChangeSuccess', function(e, curr, prev) {
// Hide loading message
$root.loadingView = false;
});
}]);
app.controller('mainCtrl',function($scope){
});
Blog Module:
var blog = angular.module('app.blog',[]);
proposal.controller('blogCtrl', function($scope, myDta){
// This is where I will handle all the logic for blogs. Will have functions calling blog service which will call the server and GET or POST data.
$scope.prop = myDta;
});
HTML:
<html lang="en" ng-app="app">
<script type="text/javascript" src="./js/app.js"></script>
<script type="text/javascript" src="./js/blog.js"></script>
<script type="text/javascript" src="services.js"></script>
Do you think architecture or structure is good enough to put into production? My app is grow slowly as I will adding a lot more functionality so I want to make sure the structure I create is maintainable and scalable.
.component()
and.directive()
and avoid$scope
where possible (since 1.5 its only required for edge cases). And really dont usengController
in your templates at all.