Book Image

Mastering AngularJS for .NET Developers

By : Mohammad Wadood Majid, Golrokh Mirzaei
Book Image

Mastering AngularJS for .NET Developers

By: Mohammad Wadood Majid, Golrokh Mirzaei

Overview of this book

<p>AngularJS is an open source framework that utilizes the Model-View-Controller architecture for client-side application development. AngularJS is referred to as the Angular framework.</p> <p>With this book, you will soon be able to build client-side data driven applications. The introduction section covers the essentials of AngularJS, covering the core concepts of AngularJS to ensure a smooth transition to the more advanced topics discussed later on.</p> <p>This book covers the development of client-side applications with AngularJS using code examples before moving on to explore how to build the ASP.NET Web API and its features using Visual Studio .NET.</p>
Table of Contents (15 chapters)
Mastering AngularJS for .NET Developers
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Understanding AngularJS


The popularity of developing client-side applications for mobile devices, such as iPhones, iPads, and Android tablets is a direct result of market penetration and a growing user base for these devices. Moreover, JavaScript, the MVC framework, and other libraries, such as Backbone.js, Node.js, and so on, have been released and adopted in the mainstream programming. In this section, we will discuss what AngularJS is and its importance.

What is AngularJS?

AngularJS is a client-side JavaScript library, which has been developed based on the MVC design pattern. AngularJS is used for client-side data binding of client-centric applications. It is open source and used by Google Inc. and its Google community. AngularJS assists to create a client-side application that needs HTML, CSS, and JavaScript. AngularJS's JavaScript framework is an effort taken to make both development and testing of application easier.

AngularJS contains additional custom HTML tags. It submits the directives in those custom HTML tags and binds the HTML elements in client side with a model's data using standard JavaScript. The values of the JavaScript variables can be set or retrieved dynamically with JSON resources.

The way AngularJS's data binding and dependency injection works is that it helps to write very less or no code for the client-side binding. AngularJS is a structural framework for dynamic application development. It lets HTML express an application's components very clearly and concisely within the browser. AngularJS proves to be a good candidate to work with the server technology and especially with ASP.NET and Microsoft's MVC.

These days, AngularJS is popular because it extends HTML with additional elements to create a template and to enable DOM for one-way or two-way binding. In real applications, in addition to data binding between view and model, executing the business logic depends on servers. AngularJS has a very rich, built-in support to communicate with the server and it also provides a built-in wrapper for communication with RESTful services.

Why AngularJS?

AngularJS proceeds by minimizing the impedance mismatch between an application's need for constructing a new HTML and document-centric HTML. Thus, as compared to other available libraries, it takes a different approach. Directives are used in AngularJS to convey to the browser how to use a new syntax. Some of the examples of the directives are:

  • Data-binding, as in {{}}

  • The two-way binding facility in AngularJS enables automatic change of either the target or source property in case one of them changes

  • Control structures for repeating and hiding a certain portion of a page

  • AngularJS sustains the client-side form validation

  • AngularJS connects the server-side code to DOM elements using JSON

  • AngularJS provides a way to group HTML to reusable components

  • AngularJS contains the Hypertext Transfer Protocol service, which can be used to communicate with remote servers

AngularJS has been developed in the belief that it can be used to solve problems that don't require the developer to specify any extra procedures.

The following are the design goals of AngularJS:

  • To separate DOM operations from the application logic. This dramatically improves the testability of the code.

  • Application testing is of equal importance to application writing. Testing is difficult and dramatically affects the way the code is structured.

  • Separation of the client side of an application from the server side. This allows development work to continue in parallel and allows reuse of both the sides.

  • During the entire lifecycle of building a client-side application, the AngularJS framework helps developers to create a user interface (UI), write business rules, debug, and test the applications.

In developing client-side applications, AngularJS is not a single piece in the overall package of the AngularJS framework. AngularJS puts the DOM and AJAX code in a well-defined structure. This makes AngularJS opinionated about how a CRUD application should be built. AngularJS has the following features and advantages:

  • Implement the CRUD operation, data-binding, basic directives, form validation, routing, deep-linking, reusable components, and dependency injection.

  • Application testing, such as unit-testing, end-to-end testing, mocks, and test harnesses.

  • Kernel application with directory layout and test scripts as a starting point.

  • The AngularJS framework is developed based on the MVC design pattern. It manages these components and connects them to server.

  • AngularJS uses additional HTML tags to create a user interface for applications. It is much easier, more natural, and less complex than creating a user interface using JavaScript. Creating a user interface with AngularJS within HTML is easy to organize; special tags in the DOM determine which controllers to use for each element. The new tags regulate what should be loaded. AngularJS directives make client-side application development easier because in AngularJS, what you see is what you get. So, instead of wasting time thinking and deciding what has to be done, one can just use the AngularJS framework to simply define what you want and what dependencies are involved.

  • The AngularJS framework uses Plain Old JavaScript Objects (POJO). Therefore, AngularJS does not need the getter or setter functions. We can directly add properties and loops into the objects. The code looks much cleaner and more organized. The traditional data model is responsible for data determination and server synchronization. AngularJS uses the data model, which uses plain objects; these objects behave like temporary storage areas to store and retrieve data. AngularJS's data model works very closely with controller and view and is known as scope. All the properties of the scope object are automatically bound to the calling view of the scope. AngularJS keeps track of the changes made to these properties and updates the view automatically when needed. There is no data in the scope, which depends on the controller to feed the data into the scope, according to the business logic.

  • The AngularJS framework brings additional functionality to the DOM by introducing supplementary tags to HTML, which are known as directives. Directives empower us to invent our own HTML tags. The MVC application can be separated by putting all DOM manipulations into the directives. This separation will permit the MVC application to only focus on updating view with new data. Directives come in the form of custom HTML tags, such as:

    <myticker></myticker> <!--<!--<!--<!--custom attributes - ->
    <div data-myticker</div> <!--<!--<!--<!--customer class names - ->
    <div class="myticker"></div> <!--<!--<!--<!--used like regular HTML elements - ->
  • Directives are designed to be standalone reusable elements separate from the application. In fact, if a particular element is adopted by the HTML5 standard, it should be as simple as removing the custom directive. The application should behave in exactly the same manner without needing to change the application. The controller should not manipulate the DOM directly. All DOM manipulations should be performed by directives.

  • The AngularJS framework provides out-of-the-box filter functions. A filter is a separate function like the directive. A filter will filter the data before the data is bound to the view. It is capable of creating a sortable HTML table without writing any JavaScript. In an application, different filters can be applied, such as "currency," which will convert a number into the currency format, "lowercase," which will convert the string to lowercase, "orderBy," which will order the list of items in the ascending or descending filter, and much more. We can also create a custom filter instead of using the out-of-the-box filter.

  • All the points up till now mean that you get to write less code. You don't have to write your own MVC pipeline. The view is defined using HTML, which is more concise. Data models are simpler to write without getters/setters. Data-binding means that you don't have to put data into the view manually. Since directives are separate from the app code, they can be written by another team in parallel with minimal integration issues. Filters allow you to manipulate data on the view level without changing your controllers. Yes, this is sort of a summary bullet point, but writing less code is a big deal!

  • Controllers in AngularJS are functions, which are used to govern the scope. For example, you will use the controller to prefill the data into the scope from the server or to implement business logic validations. In contrast to alternative frameworks, controllers are not objects and there is no inheritance involved. If controllers are easy, then where the work ought to be performed? AngularJS introduces services to do just that. Services are specifically what they sound like. They should not get entangled with the MVC of your application by merely offering associate outward API to reveal no matter what you wish it to reveal. Most of the time, the service syncs up to a server to keep up the associate offline data store and exposes ways to push and pull information to and from a server. AngularJS produces a resource sharing service that enables multiple controllers to share an equivalent resource. Services are designed to be standalone objects that cut loose your application. They permit your controller to stay lean and dedicated to the view and scope that they are assigned to. Services are mainly used to replace the concept of class in object-oriented programming. Of course, implementation of services is not needed and it's utterly acceptable to try and do some lightweight lifting within your controller to avoid over complexness.

  • A PubSub system is a pretty common tool that permits decoupled communication. A PubSub patterned communication between a publisher (sender) and subscriber (receiver) involves transmitted messages characterized as classes, instead of being passed as entire messages. Most PubSub implementations on the Web are not aware of the context. Typically, you would wish a PubSub message to be only legible to the children of a selected node or solely readable by the ancestors of a selected child. In other words, typically, you do not need unrelated MVC parts to read your messages. The PubSub system in AngularJS is exactly that. The broadcast() perform can send a message to all or any child controllers, whereas the emit() perform can send a message to all or any ancestors. However, PubSub is not the only way to communicate with controllers. In fact, if all you are doing is telling different controllers to update their views once a property changes, you must want data binding. However, what I did not tell you is that scopes inherit the properties of their parent scopes. This means that if a property exists on the parent scope and a child scope modifies it, then all different scopes that inherit from the constant parent will see constant modification; their views will be updated mechanically by the current version of AngularJS.

  • The whole of AngularJS is coupled along by Dependency Injection (DI). This is what it uses to manage your controllers and scopes. As a result, all of your controllers depend on DI to pass information in order to perform unit testing by injecting mock information into your controller and measuring the output and behavior. In fact, AngularJS already incorporates a mock communication protocol supplier to inject faux server responses into the controllers. This beats the additional ancient approach of taking a look at the acting Internet applications by making individual test pages that invoke one part, then interacting with it to envision whether or not it works.

The preceding points should provide ample explanation as to why AngularJS is so useful and powerful. Not all web apps use AngularJS. For instance, if you're writing a game or a computationally intensive mathematics program, there's no reason why AngularJS would suit your explicit downside domain. Except for generic Internet apps, it ought to function as a viable framework.