Menu

Monday, 12 January 2015

Quick Start Of AngularJS

About 
Angular JS is the one of the finest JavaScript frameworks to create dynamic web applications by writing less code with an easy syntax in HTML files.
RoadMap
In this article you will get an idea of the following concepts.
  1. Introduction
  2. Prerequisites
  3. Download Angular Js
  4. Angular Js Editors
  5. Angular Js Architecture
  6. About ng-app and Binding Expressions
  7. ng-controller
  8. ng-model
  9. ng-show and ng-hide 
Introduction
Angular JS is the one of the best frontend frameworks for implementing any kind of dynamic web application using JavaScript and HTML for the following reasons.
  1. It helps you to organize your JavaScript code by creating controllers, modules and so on.
  2. The website creation is very fast, as fast as adding attributes to the controls.
  3. It is compatible with jQuery as well.
  4. It's suitable for testing the application since the way of writing JavaScript is organized.
Angular JS started as a Google project but now it is an open-source project. So it's available in GitHub and anyone can contribute to it.
The official site for Angular JS.
Until now, there are many applications developed using Angular JS. You can get a list of all those applications from the URL Buit With AngularJS.
In Angular JS web applications, using Directives you can tell HTML to trigger your JavaScript functions.
Angular JS provides many directives, even though most of the directives are understandable by the name itself. The directives start with “ng-”. Here ng refers to and is pronounced as Angular.
Angular JS has no other dependencies. So you just need to include the JavaScript file whereever required.
Prerequisites
To implement web applications using the frontend application framework, you just need to have some idea of the following concepts:
  1. HTML
  2. CSS
  3. JavaScript
Download AngularJS
The Angular js files you can download from AngularJS. You can even use the CDN URL to access the JavaScript file.
The CDN URL is: https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js
AngularJS Editors
There are many suitable editors available (online/offline) to implement or practice Angular JS applications. They are the following:
  1. JS Fiddler (JSFiddle ) and so on.
AngularJS Architecture
AngualrJS should be categorized as pinny needed MV* framework and it also supports Unit tests and both integrated end to end tests.
Model: the Model is used to restore the data and state of your application.
View: Where you actually render to the user the information they want.
It stands like any other Controller / Presenter / ViewModel and so on. Even though in most of the cases we will use the controller.
So, Controllers are the main components in Angular applications, Angular JS supports two-way binding. That means that whenever the user enters into the form fields they are instantly updated in Angular models.
So, let's start a sample application using Angular JS to get a brief idea of it.
Note: in these examples, I am using Angular Js 1.3.8, the latest version, since this version contains many features and at the same time, some of the features that are supported in 1.2.x would not be supported in the 1.3.8 version (for example, Global controller functions are not supported in 1.3.x by default).
ng-app and Binding Expressions
In this example, you will implement a basic application using Angular JS.
  
  
  
  
    
    
  
  
  
  

Welcome to Angular Application

This div is controlled by Angular JS: {{ 1 + 2 }} To Display Constants / Static ones in Angular JS: {{ '1 + 2' }}
Normal div doesn't handled by Angular JS: {{ 1 + 2 }}

The previously described HTML file contains five notified points as specified in the comments section of HTML.
The Angular JS script CDN reference added at the head of HTML file.
ng-app: The directive ng-app is added as an attribute to one of the div with an Id of AngularDiv. It is a special directive since whenever it is found inside a HTML page it starts the action of managing the page.
{{ }}: This format or notation is called Binding Expressions in Angular JS.
So, whenever you have defined some expression inside the Binding Expressions ({{ }}) that is controlled by AngularJS directive (ng-app) automatically it evaluates and displays the results in an HTML page.
If you want to display an expression within binding expressions ( {{ }} ) without evaluation then you need to enclose it with single quotes as described in the 4th point of the HTML file.
The other div with the Id NormalDiv does not have an assigned attribute (ng-app). So it displays data normally without evaluating the data within the binding expressions.
Now, you can save the HTML file and run it in a browser and the output will be displayed as follows.

You can run the sample application using the Plnkr tool at AngularJs Starter .
Until now, you got an idea of the special directive named “ng-app” and how you can use the binding expressions to evaluate the data. Now, you can move to the next step of writing JavaScript code and trigger that code in the HTML file using Angular JS directives.

ng-controller
The primary responsibility of the controller is to create the scope object and that scope can communicate with the view in both directions. You can bind the properties and functions to the scope object.

  
  
  
  
    
    
    
    
  
  
  
  

{{ angularjs }}

The HTML file contains mainly the following 3 points:
  1. Angular Js script file CDN path reference.

  2. Demo.js file reference containing a few lines of JavaScript code.

  3. ng-app refers to the module name (DemoApp), it refers to any controllers created under it that is part of that module. Ng-controller refers to the controller name (DemoController). 
The Demo.js file contains the following lines of JavaScript code.

var WelcomeAngular = function($scope) {  
  $scope.angularjs = "Welcome to Angular Js";  
};  
  
var app = angular.module("DemoApp", [])  
app.controller('DemoController', WelcomeAngular);


The first step of JavaScript file contains one variable named WelcomeAngular. It refers to one function with the scope object. The scope object contains one property named angualrjs.
In the second step you are creating an Angular module named “DemoApp” to the variable “app” and then assigning a controller named “DemoController” to that app.
You can now run the HTML file and then you will see the output as “Welcome to Angular Js” that was specified in the JavaScript file to the property named angularjs in the scope object.
You can run the sample application using the Plnkr tool at  AngularJs Controller.

ng-model
ng-model is also one of the directives, it can bind to the input, select or custom form controls using the property named ng-model.
  
  
  
  
    
    
  
  
  
  

Employee Search

You entered employee name is: {{empname}} 
  1. Added the Angular JS CDN path.
  2. ng-app directive was included in the body field. So the HTML body is under Angular JS.
  3. ng-model is added to the input field with the value empname.
In this example, as you notified the only new one is ng-model with the value in the input field. So, whenever you have added the ng-model property with the value (empname) and where you are using that value (empname) automatically it populates by entering the text into the respective input field.
Initially, when you run the HTML file, it is displayed like the following.

Now, whenever you have entered some value into the TextBox those values are automatically shown in the screen as output asynchronously.

You can run the sample application using the Plnkr tool at AngularJs Model.

ng-show and ng-hide
ng-show and ng-hide are also directives. These are very helpful when you want to hide or show something based on a property or object that either contains a value or does not.
As you seen in the aforesaid model example, the string is always displaying like "you entered employee name is:". You can change it so it will only display whenever the user has entered a value into a text box field.
For example, when you search for employee details, you need to display some message when the user begins searching by entering some employee name.

  
  
  
  
    
    
  
  
  
  

Employee Search

Employee Search Started..!!!
You enetered employee name is: {{empname}}
The previously described HTML file contains the following two pre tags:
  1. The first one contains the ng-show directive with the value empname. So, The pre tag will be displayed whenever you have entered some text into the input filed that has applied the ng-mode is empname.
  2. The second one contains the ng-hide directive with the value !empname (here ! Indicates not). So, The pre tag will be hidden when the empname is empty.
Note: in both of the preceding cases you can use any one of the directives (ng-show or ng-hide) for simplicity. But for the purpose of understanding, I have used both directives.
Now, you can run the HTML file and by default it displays like the following.



Now, you can enter any of the text in that text box field and then the screen will have the results shown as follows.


 You can run the sample application using the Plnkr tool at AngularJS Show Hide.

You can download the aforesaid all examples from this link AngularJS Examples.

Conclusion
I hope this article provides you an idea of the history and architecture of AngularJS, Binding Expressions and some of the directives like ng-app, ng-controller, ng-model, ng-show and ng-hide.



5 comments:

  1. Come and read us!! We are moving our blog into a new site with a much more pretentious goal. We are going to teach how to be AngularJS Ninjas!!! That's right! We have taken a couple of weeks to prepare our first workshop, absolutely free!!!!
    AngularJS Training in Chennai
    AngularJS Corporate Training in Chennai
    AngularJS Online Training

    ReplyDelete
  2. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    https://www.emexotechnologies.com/courses/other-technology-trainings/python-training

    https://www.emexotechnologies.com/courses/big-data-analytics-training/data-science-with-python-training

    https://www.emexotechnologies.com/courses/cloud-computing-training/amazon-web-services-aws-training

    https://www.emexotechnologies.com/courses/big-data-analytics-training/big-data-hadoop-training

    https://www.emexotechnologies.com/courses/other-technology-trainings/devops-training

    https://www.emexotechnologies.com/courses/web-development/angular-js-training/

    ReplyDelete
  3. AngularJS course in Noida
    https://aptronsolutions.home.blog/2022/08/02/whats-new-in-angular-14-heres-a-quick-look-at-the-features-updates/

    ReplyDelete