Introduction
In this article, we will discuss about a modular way to develop AngularJS single page applications for better maintenance. The example code demonstrates sign up and login functionality and you can improvise it as per your need. It is available here.
Background
You should have knowledge of the following technologies for understanding the code.
- AngularJS 1.5 - used for the business logic of the application.
- Asp..NET MVC 5 - used as a wrapper around Angular code.
- TypeScript 2.0 - used to implement the OOP easily in Angular code.
- Asp.net Web Api 2 & Oauth - used for server side business logic of the application.
- Mongodb - used to store data. (optional)
- Repository pattern - used for decoupling a data access layer from the application. (optional)
- MEF (optional)
Take Away from this article
- Developing AngularJS as Mini SPA
- Use of AntiforgeryToken and Token based authentication and authorization for Web API 2
- Use of OOPS in AngularJS using TypeScript.
- Use of ASP.NET MVC for reducing the complexity of a big application.
- Developing the SPA in modular style.
Drawbacks of traditional SPA approach
- Difficult to maintain a big application since everything gets added literally in a single page/file.
- Single Routing file becomes bigger and difficult to maintain.
- Single Home page stores all application references like CSS and JS files.
- Understanding and maintaining the existing application becomes very difficult; changes can lead to more bugs.
- Cannot use approach like ASP.NET MVC AREA where we can separate functionality and the code.
- Mixing Angular code and ASP.NET MVC is not a good idea. (This approach is very inflexible and sometimes writing unit test cases for Angular code becomes quite tricky).
- Cannot take advantage of OOPS.
Advantages of my approach
- Instead of developing a big SPA, break the functionality into multiple Mini SPAs.
- Using TypeScript for implementing OOPs in AngularJS code.
- Separate routing file for each mini SPA.
- Separate ng-app module for each mini SPA and the module can be reused in the other modules.
- Separate homepage for each mini SPA, which can hold only required reference files.
- A module can be used as an app module/root module in one mini SPA as well as can be used as a normal module in another mini SPA.
- Using ASP.NET MVC as a wrapper just to load required references and to bundle the reference files and for mini SPA’s homepages.
- Basic authentication can be done in UI project since ASP.NET MVC is wrapper and each feature (mini SPA) can be guarded.
Suppose, we are developing a dating site which has the following features.





Faisal AhmedPosted Jun 18, 2018, 9:23 AM
Thanks for this helpful article.
Vinod AbrahamPosted Apr 26, 2017, 3:12 AM
Thanks for the nice article. could you please share the source code. below link is not working https://github.com/vicharemakrand/Sample.MVC_Angular_WebApi_Mongo
Sandeep ChPosted Feb 4, 2017, 8:53 PM
Good Info. Thanks for sharing with you detailed explanation with oops concepts in angular .
Alex LiPosted Jan 18, 2017, 8:29 PM
Nice share,Where can I have the source code?
Satish Kumar VadlavalliPosted Dec 14, 2016, 4:59 AM
Nice share, keep up good work
Fabio Silva LimaPosted Dec 13, 2016, 7:34 PM
Cool stuff, it's close to my angularjs and mvc aproach.