What is AngularJS?
I tried to answer this question in my previous article and to summarize
it all, AngularJS is a client-side JavaScript framework to add
interactivity to HTML.
In this article I'll try to introduce you to the various terms commonly used when developing Angular applications. They are: Directives, Modules and Expressions. Other terms that I ll be covering later are: Controllers, Services and so on.
As we go deeper into it, the first question that arises in my mind is, “How do we tell our HTML when to trigger JavaScript?”.
The answer is: through directives.
Directives
A directive is a marker or attribute written along with the HTML tag that tells AngularJS to run or reference some JavaScript Code.
We have an HTML snippet like:
In this article I'll try to introduce you to the various terms commonly used when developing Angular applications. They are: Directives, Modules and Expressions. Other terms that I ll be covering later are: Controllers, Services and so on.
As we go deeper into it, the first question that arises in my mind is, “How do we tell our HTML when to trigger JavaScript?”.
The answer is: through directives.
Directives
A directive is a marker or attribute written along with the HTML tag that tells AngularJS to run or reference some JavaScript Code.
We have an HTML snippet like:
- <!DOCTYPE html>
- <html>
- <body>
- …
- </body>
- </html>
And a JavaScript function like:
- function appSection() {
- alert (“Hello World!”);
- }
How do I tell AngularJS to run this JS method at a certain point. We do it with directives.
- <!DOCTYPE html>
- <html>
- <body ng-controller=“appSection”>
- …
- </body>
- </html>
Here
"ng-controller" is the attribute that tells AngularJS to run the method
"AppSection" and will result into an alert message as soon as the
document tag <body> is rendered.
This is how we bind an element with its behavior.
Modules
Moving on in the direction of making the application in an "Angular" way, the next step is to keep it encapsulated with relevant "Modules".
Modules are the sections where we write pieces of an Angular application. They help us make our code more maintainable, testable and readable.
We define dependencies of our application into the modules only. We can also use several modules as the dependencies for another module.
Writing a module
This is how we bind an element with its behavior.
Modules
Moving on in the direction of making the application in an "Angular" way, the next step is to keep it encapsulated with relevant "Modules".
Modules are the sections where we write pieces of an Angular application. They help us make our code more maintainable, testable and readable.
We define dependencies of our application into the modules only. We can also use several modules as the dependencies for another module.
Writing a module
- var app = angular.module('myApp', []);
Here, "myApp" is the name of the newly created module and the [ ] contains the array of dependency names.
In our way to bring this module to life, we need to add a new directive "ng-app" in our HTML. It can be done this way:
In our way to bring this module to life, we need to add a new directive "ng-app" in our HTML. It can be done this way:
- <!DOCTYPE html>
- <html ng-app=”myApp”>
- <body>
- …
- </body>
- </html>
The
module "myApp" isn't doing anything yet, but just by including it into
our HTML with an element, its going to treat the HTML inside of this
element as a part of Angular application and this means, we can now move
on to writing expressions.
Expressions
Expressions are how we include dynamic values into our HTML page.
Some of the basic ones are:
Expressions
Expressions are how we include dynamic values into our HTML page.
Some of the basic ones are:
- <p> I am {{ “Shashank” + “Srivastava” }} </p>
- is going to render out as:
- <p> I am Shashank Srivastava </p>
- <p> I am {{ 12 + 12 }} </p>
- is going to render out as:
- <p> I am 24 </p>
These were some of the keywords one should understand while stepping into the world of AngularJS.
While I extend this topic any further, I'll just wire up all three of these directives, modules and expressions into a single page application. Please take a look at it to get the best of it: jsfiddl
While I extend this topic any further, I'll just wire up all three of these directives, modules and expressions into a single page application. Please take a look at it to get the best of it: jsfiddl
Source collected from :http://www.c-sharpcorner.com/UploadFile/2776f9/angularjs-basics/
No comments :
Post a Comment