5 Steps to Improve a Basic AngularJS Application

AngularJSAngular.js is a client-side javascript framework developed and maintained by Google.

It is a MVW ( Model View Whatever ) framework.

It is introduced to make static html dynamic. In other words, it updates the view of the web application in sync with any change in the data ( Model ). It makes the view data driven.

It gives us the flexibility to choose the design pattern that we want to use in our web application.

Here, ViewModel contains the presentation logic which distinguishes it from MVC pattern. Moreover, angular.js creates two way data binding, i.e. any changes in data object reflects on the view at the same time any changes in the view get updated in data object. So, it is a good choice when it comes to the development of single page applications where page redirects or page refreshing are not entertained.

In MVC ( Model View Controller) pattern, the application is designed such that Modal contains the data as well as the business logic, View contains the presentation script ( html tags) and controller is a mediator between Modal and view. This pattern is generally used with the server side scripting languages like PHP.

However, in client side framework, MVVM (Modal View ViewModel) pattern is often used and it can be accomplished by using angular.js.

The following basic example app gives an high level overview on how to get started on Angular JS development.

1. Include AngularJS Script

You have two options here.

You can download the latest version of the angular.js and add it to your view script.

Or, you can use the hosted library from google using the URL mentioned below in the script section.

AngularJS basic application example by thegeekstuff.com

2. Bind ng-app Attribute

In this example, we are binding a module called “someApp” using “ng-app” directive to the div element.

In this step, bind ng-app attribute (directive) to a module name in html tag where we want to bootstrap the angular app. We’ll create the module in the next step.

AngularJS basic application example by thegeekstuff.com

3. Create an Angular Module

Next, create an angular module and add it to your view.

This keeps the application functions are variables separate from the global namespace.

You can also write it in a separate javascript file and add it to your view using script tag. In the example below its written inline.

AngularJS basic application example by thegeekstuff.com

The second argument to the module is an array of module dependencies that we may have.

4. Create a Controller

In the example below, we are creating a controller by chaining method.

This is some paragraph

Please note in the above code we have used “$scope” object in the controller which is available in the view to access. It thus stitches the controller and the view.

5. Define Controller Scope

Finally, use ng-controller directive in html tag to define the scope of the controller.

{{text}}

In the above code we are accessing the scope object variable using angular expression using handlebar template binding {{ }}.

Copy and paste the above script into a html file and open it in any browser to see the AngularJS in action.

You will see the text “AngularJS basic application example by thegeekstuff.com” rendered on your browse.

This text is basically set in the application model and is driven by data on the view which is in the controller scope.

To learn more about angular directives, filters, expressions and services refer to the AngularJS documentation.

>

You may also like...