Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Javascript

Beginning AngularJS

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
16 Feb 2015CPOL5 min read 14K   10  
Beginning AngularJS

The post Beginning AngularJS appeared first on codecompiled.

When developing web applications, HTML is the main markup language irrespective of other web development technologies such as ASP.NET .HTML is a declarative language designed for describing the documents which can be rendered by a browser.

The web applications of today are much more than a group of static documents.Today, the web applications are completely dynamic, this means that the UI needs to be updated immediately in response to the user actions. In contrast, HTML is designed for displaying static documents.

This difference between HTML and the web applications of today is also because of the latest changes in web development technologies. HTML’s main objective is displaying static documents, it's not suitable for developing rich UI applications in which client is expected to provide more fluid user experience.

This difference between rich User Interface and HTML leads to the developer manipulating the DOM programmatically. So for creating a rich user interface, a developer needs to perform the following tasks, whenever user interacts with our application.

  1. Make an Ajax call to the server.
  2. Format the return data if required in the required format.
  3. Manipulate the HTML DOM to render the returned data.

angularjs page refresh

These are the common tasks which need to be performed again and again in any rich UI application. So instead of performing these tasks manually, wouldn’t it be nice if we can use a framework for these tasks? This is where frameworks such as AngularJS are used.

AngularJS is a JavaScript framework for creating web applications which provide a rich user interface similar to a desktop application. These types of applications are known as Single Page Applications.

In an SPA application, the web page is never reloaded. In the initial request, the HTML, JavaScript and other client resources are sent in the request. As the user interacts with the application, data fetched is from the server, this client server interaction happens asynchronously behind the scenes. Because of this, it provides a user experience similar to a desktop application.

SPA Application architecture

In an SPA application, all the required HTML, JavaScript and the CSS are loaded on the first page load and a request is made to the server only for fetching the necessary resources such as the data. As the user interacts with a single page rather than different pages, hence it is called Single Page Application.

As most of the application logic is moved from the server to the client, client side frameworks such as AngularJS have an important role in SPA applications.

AngularJS applications are structured using the MVC pattern, the application is structured into Mode, View and the Controller.

Model: Model is typically a JavaScript object and is populated by retrieving values from the server. If our model grows in complexity, we can create model using factories so that the task of creating model object is separated from the controller. If we have a simple model object, we can directly define the model object using JavaScript in controller. The data for the model object is typically retrieved from the web server.
To bind the input element in the view to our model, we can use the ng-model directive on the element, e.g., ng-mode=”model name”.

View: View is just a HTML template which renders the model. Views in AngularJS are created by extending the HTML with directives. AngularJS directives are just HTML attributes with prefix ng-. These directives give special instructions to the Angular application about processing the HTML elements.

Some of the commonly used directives in angular are:

  • ngApp: Defines the scope of the angular application
  • ngController: A controller object is created using the controller function. In the controller function, we set the state and behaviour of $scope.
  • ngBind: This directive is used to set the text content of the HTML element with the value of the ng-Bind expression.
  • ngModel: Binds the property of $scope to the HTML element.
  • ng-Repeat: Creates a template for each item in a collection. We can use ng-Repeat as:
    <div ng-repeat="item in Collection">
    <div>item</div>
    </div>

Controller: Controller is the component which binds the model to the view. Controller in AnguarJS has two impoartant uses:

  • Sets the Model for the view
  • Contains the business logic

It’s a function which is automatically passed a $scope parameter. $scope parameter sets the model for the view. View can access this $scope object to render the model.

If our model grows in complexity, we can create model using factories so that the task of creating model object is separated from the controller. If we have a simple model object, we can directly define the model object using JavaScript in controller. The data for the model object is typically retrieved from the web server.
To bind the input element to our model, we can use the ng-model directive on the element as ng-mode=”model name”.

SPA CONTROLLER $SCOPE

Model binding in Angularjs is two way, the changes in UI are automatically updated in the view and when the user updates the view, changes are reflected in the model. This is a big benefit since we don’t need to sync the data between the model and the view. Data binding expressions have the syntax as {{expression}} and we can insert the expression in html wherever we want to display the value of the model data.

In the following example, we are consuming the model data using both the data binding {{expression}} and the ng-model directive.

HTML
<body ng-app >
<form name="myForm" ng-controller="employeeController">
<span> <b>Employee Details</b></span>
<table>
<tr> <td>Id: {{Id}}</td> <tr>
<tr> <td>Name: {{EmployeeName}}</td> <tr>
<tr> <td>Address: {{Address}}</td> <tr>
<tr> <td> EmpoymentType<input 
ng-model="EmpoymentType"></input> </td> <tr>
</table>
</form>
</body>

We can define a controller as a function.The controller function is passed a $scope object explicitly. To set the model values for the view defined above, we can use the following controller function. To begin using AngularJS library, we just need to reference the angular.min.js file.

HTML
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
function employeeController($scope) {
$scope.Id = "1";
$scope.EmployeeName = "SuperUser";
$scope.Address = "Address1";
$scope.EmpoymentType = "Permanent";
}
</script>

If we run the above application, we get the following page:

angularjs directives

AngularJS has become quite popular in the last few years as a framework for developing Single Page Applications.There are other popular frameworks for developing Single Page applications such as DurandalJS, Ember.js. Advantage of AngularJS is that we can use our existing knowledge of HTML and use directives to extend it instead of learning a totally new syntax. Directives are just HTML attributes which we apply to different HTML elements.

Using AngularJS in our web applications provides better user experience as the UI can be updated without making a round trip to the server.

The post Beginning AngularJS appeared first on codecompiled.

This article was originally posted at http://www.codecompiled.com/beginning-angularjs

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I have a keen interest in technology and software development.I have worked in C#,ASP.NET WebForms,MVC,HTML5 and SQL Server.I try to keep myself updated and learn and implement new technologies.
Please vote if you like my article ,also I would appreciate your suggestions.For more please visit Code Compiled

Comments and Discussions

 
-- There are no messages in this forum --