Tutorial for Building SPA using AngularJS

Recently I’ve been working on and evaluating different JavaScript frameworks for the next big project I’ll be involved in, we want to build Single Page Application (SPA) while depending on a solid JavaScript framework, so I decided to get my hands dirty and try to build a demo application using one of the solid JS framework, and the decision was AngularJS!

I called the demo app “Foursquare Explorer”, it is built using Twitter Bootstrap, Web API, Foursquare API, and for sure AngularJS. I was amazed how fast I was able to produce functional and neat app.

Foursquare Explorer Web Application

Foursquare Explorer Web Application

At the beginning of the learning phase, I tried to read different articles and blog posts about Angular but I got lost and confused, until I watched this short 60 minutes tutorial done by Dan Wahlin, You won’t regret watching it if you are new to Angular.

Before discussing what we’ll build during this tutorial I would like to talk about some of the features and concepts of Angular:

  • Two-way data binding:

AngularJS templating engine binds the data in two way, this means that it is auto-magically synchronize the data between the view and the model, so if you updated the view then the model is updated and vice verse. This feature will cut out lot of the code needed to update your DOM. You can check this out using this plunker live demo.

Two_Way_Data_Binding

  • Declarative v.s Imperative user interface:

AngularJS uses HTML to define user interface via Directives, those directives are responsible to set event handlers behind the scene (Directives detail below). So you have to question yourself when you start setting IDs for html elements and try to manipulate the DOM using Javascript or jQuery.

  • Models are POJO:

Data models in AngularJS are just “Plain Old JavaScript Objects” without any setters or getters. Any changes done on the view are immediately pushed back to the model, thanks for the two–way data binding and the “scope” object.

  • Scopes:

Scopes in AngularJS can be considered the glue between the controller and the view, controller should not know anything about the view, and the view can know about the controller via the scope object.

AngularJS scope

  • Controllers:

Controller in AngularJS is function with two main responsibilities which they are implement business logic, and manipulating the scope, not the HTML DOM for sure.

  • Directives:

Directives in AngularJS will let you teach HTML DOM elements what to do, you will be able to enhance the capabilities of DOM elements by creating your custom directives and promote code reuse.

  • Services:

Services in AngularJS are singleton objects, they are responsible for the heavy lifting in your app, most of the time they provide methods responsible to push/pull data from the back-end server or services. As well you can use services to share common logic between different controllers.

  • Filters:

Filters will enable you to filter the data before it is projected to on the view, it might involve something straight forward such as date or numbers formatting, or you can implement your own custom filters such as filtering data source.

I feel you now, I believe you are confused with those new concepts! But do not worry, we’ll cover those concepts deeply during this tutorial while we are building the app.

What we’ll build in this tutorial?
I’m big fan of Foursquare, I’ve been always using their iPhone app for finding cool places with good rating; so I decided to build sample web application which allows us to explore places by providing your city i.e.(Amman) and optional category, then you will be able to bookmark places you like to visit them later!

Use cases we will cover in this web app are:

  1. Explore places based on City (Mandatory) and Places Category (Optional).
  2. Display 10 places on the view and enable server side pagination so we keep our app snappy.
  3. Displaying the top 9 image thumbnails of the place as a popover.
  4. Allow users to bookmark the place for future visit. (For the sake of simplicity we will use username without password and if you refreshed the page the user context is flushed).
  5. Displaying the bookmarked places for users.
  6. Finally we want cool UX and UI which will adopt to different screen sizes (Response Design) so we will use bootstrap UI as it plays nicely with AngularJS.

I’ve decided to breakdown this series to three posts, posts are:

Update (2014-May-5) New post which covers adding GulpJS as Task Runner for our AngularJS application:

Update (2014-June-1) New post which covers Implementing AngularJS Token Authentication using ASP.NET Web API 2 and Owin middleware:

We’ll build this app incrementally so we will add feature by feature, the source code for this series is available on GitHub, feel free to download it locally or fork it, I’m open to ideas and suggestions to add more features to this app, Foursquare API is great and we can build many features on top of it. As well you can try a demo of the app.

If there is nothing clear or ambiguous please drop me a comment and I’ll do my best to reply your questions.

To get the best of this tutorial I recommend you to follow the posts one by one. Hope you’ll enjoy coding with AngularJS as much as I enjoyed :)

Source code is available on GitHub. Demo application is hosted on WindowsAzure.

Comments

  1. Haider says

    I am new AngularJS and SPA. Can you please update your app by adding security so that for each call, whether its for getting HTML view from MVC controller or data from API controller, user get authenticated/authorized first.

  2. Mark says

    I got a source code, opened solution in VS 2013 and tried running I got an error message :

    Server Error in ‘/’ Application.
    ——————————————————————————–

    The resource cannot be found.
    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /

  3. Mark says

    Can you please explain how an AngularJS controller communicates with an MVC (c#) controller to get data? If that’s what they do. Thanks

  4. says

    Hi Taiseer,

    Thanks for the great tutorial. Always helps to have working examples to jump into as you gain knowledge.

    Just a “heads up”, but the FoursquareAngularJS.Web (others?) example code uses angular ui-bootstrap-tpls-0.9.0.js and works just fine. However, when I use the latest version (ui-bootstrap-tpls-0.11.0-tpls.js), I get an error in the pagination section:

    Object # has no method ‘$render’ at line 2207:19

    I’m new to Angular, so I can’t offer anything more than letting you know there might be changes which may effect your demo.

    Anyway, thanks again.

    • says

      Hi Dwight, glad you liked it, and it is always better to have working example so readers can play and test it out.
      I know about this issue, the API for data-pagination in Angular Bootstrap UI 0.11.0 version has been changed, they simplify it more. I might update this soon once I’ve the time.
      The fix for this is to change the controller as the below:
      $scope.pageChanged = function () {
      getPlaces();
      };
      And the properties in the that should be changed for data-pagination directive as below:
      data-ng-change=”pageChanged()” data-ng-model=”currentPage”
      Didn’t test it out but it should work.

Trackbacks

Leave a Reply