Introduction
In this article we will see how to use Angular.js in a LightSwitch application. Please check the previous articles on LightSwitch by visiting the following links.
Why AngularJS?
  1. It decreases emphasis on directly handling DOM manipulation from the application logic
  2. It employs efficient two-way data binding and sensible MVC implementation
  3. We can create our own HTML custom tags and attributes
  • Step 1: Let's start with a new LightSwitch application
Create a new project by opening Visual Studio 2013 then selecting "File" -> "New" -> "Project..." then create a new "LightSwitch HTML Application" and name it "LightSwitchAngularJs".
LightSwitchHTMLApplication
  • Step 2: Right-click on the Data Sources folder and select Add Table.
Data Sources folder
Create a table called Products and save it. The table will be pluralized to Store.
table
  • Step 3: Now let's add Nuget components.
Go to "Tools" and select "Library Package Manager" and then select "Package Manager Console".
Package Manager Console
And then type one by one and install.
  1. PM> Install-Package angularjs
  2. Install-Package Microsoft.AspNet.Mvc -Version 5.0.0
  3. PM> Install-Package AspNetWebApi
install
  • Step 4 Create a folder named "App_Start" in the server application and add the following class files.
class files
Right-click on the "App_Start" folder and select "Add" then select "Add" -> "New Item". Create a file called "WebApiConfig.cs" and add the following implementation:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Http;
  6. namespace LightSwitchApplication
  7. {
  8. public static class WebApiConfig
  9. {
  10. public static void Register(HttpConfiguration config)
  11. {
  12. config.Routes.MapHttpRoute(
  13. name: "DefaultApi",
  14. routeTemplate: "api/{controller}/{id}",
  15. defaults: new { id = RouteParameter.Optional }
  16. );
  17. }
  18. }
  19. }
Also, (in the same App_Start directory) create a file called "RouteConfig.cs" and use the following implementation:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7. namespace LightSwitchApplication
  8. {
  9. public class RouteConfig
  10. {
  11. public static void RegisterRoutes(RouteCollection routes)
  12. {
  13. // This rule is required to allow the LightSwitch OData service to
  14. // be accessed when WebAPI is also enabled
  15. routes.IgnoreRoute("{*allsvc}", new { allsvc = @".*\.svc(/.*)?" });
  16. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  17. routes.MapRoute(
  18. name: "Default",
  19. url: "{controller}/{action}/{id}",
  20. defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  21. );
  22. }
  23. }
  24. }
Again, (in the same App_Start directory) create a file called "FilterConfig.cs" and use the following code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace LightSwitchApplication
  7. {
  8. public class FilterConfig
  9. {
  10. public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  11. {
  12. filters.Add(new HandleErrorAttribute());
  13. }
  14. }
  15. }
Also, (in the same App_Start directory) create a file called "BundleConfig.cs" and use the following code:
  1. using System.Web;
  2. using System.Web.Optimization;
  3. namespace LightSwitchApplication
  4. {
  5. public class BundleConfig
  6. {
  7. // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
  8. public static void RegisterBundles(BundleCollection bundles)
  9. {
  10. }
  11. }
  12. }