Angular.JS Playbook - ajayupreti/Tech.Blogs GitHub Wiki
AngularJS (commonly referred to as "Angular.js" or "AngularJS 1.X") is a JavaScript-based open-source front-end web application framework mainly maintained by Google and by a community of individuals and corporations to address many of the challenges encountered in developing single-page applications.
Before you study AngularJS, you should have a basic understanding of:
- HTML
- CSS
- JavaScript
-
AngularJS version 1.0 was released in 2012.
-
Miลกko Hevery, a Google employee, started to work with AngularJS in 2009.
-
The idea turned out very well, and the project is now officially supported by Google.
AngularJS is an open source web application framework. It was originally developed in 2009 by Misko Hevery and Adam Abrons. It is now maintained by Google. Its latest version is 1.4.3.
Definition of AngularJS as put by its official documentation is as follows โ
AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. Angular's data binding and dependency injection eliminate much of the code you currently have to write. And it all happens within the browser, making it an ideal partner with any server technology.
-
AngularJS is a powerful JavaScript based development framework to create RICH Internet Application(RIA).
-
AngularJS provides developers options to write client side application (using JavaScript) in a clean MVC(Model View Controller) way.
-
Application written in AngularJS is cross-browser compliant. AngularJS automatically handles JavaScript code suitable for each browser.
-
AngularJS is open source, completely free, and used by thousands of developers around the world. It is licensed under the Apache License version 2.0.
-
Data-binding โ It is the automatic synchronization of data between model and view components.
-
Scope โ These are objects that refer to the model. They act as a glue between controller and view.
-
Controller โ These are JavaScript functions that are bound to a particular scope.
-
Services โ AngularJS come with several built-in services for example $https: to make a XMLHttpRequests. These are singleton objects which are instantiated only once in app.
-
Filters โ These select a subset of items from an array and returns a new array.
-
Directives โ Directives are markers on DOM elements (such as elements, attributes, css, and more). These can be used to create custom HTML tags that serve as new, custom widgets. AngularJS has built-in directives (ngBind, ngModel...)
-
Templates โ These are the rendered view with information from the controller and model. These can be a single file (like index.html) or multiple views in one page using "partials".
-
Routing โ It is concept of switching views.
-
Model View Whatever โ MVC is a design pattern for dividing an application into different parts (called Model, View and Controller), each with distinct responsibilities. AngularJS does not implement MVC in the traditional sense, but rather something closer to MVVM (Model-View-ViewModel). The Angular JS team refers it humorously as Model View Whatever.
-
Deep Linking โ Deep linking allows you to encode the state of application in the URL so that it can be bookmarked. The application can then be restored from the URL to the same state.
-
Dependency Injection โ AngularJS has a built-in dependency injection subsystem that helps the developer by making the application easier to develop, understand, and test.
-
AngularJS provides capability to create Single Page Application in a very clean and maintainable way.
-
AngularJS provides data binding capability to HTML thus giving user a rich and responsive experience
-
AngularJS code is unit testable.
-
AngularJS uses dependency injection and make use of separation of concerns.
-
AngularJS provides reusable components.
-
With AngularJS, developer write less code and get more functionality.
-
In AngularJS, views are pure html pages, and controllers written in JavaScript do the business processing.
-
Not Secure โ Being JavaScript only framework, application written in AngularJS are not safe. Server side authentication and authorization is must to keep an application secure.
-
Not degradable โ If your application user disables JavaScript then user will just see the basic page and nothing more.
The AngularJS framework can be divided into following three major parts โ
-
ng-app โ This directive defines and links an AngularJS application to HTML.
-
ng-model โ This directive binds the values of AngularJS application data to HTML input controls.
-
ng-bind โ This directive binds the AngularJS Application data to HTML tags.
Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts โ
-
Model โ It is the lowest level of the pattern responsible for maintaining data.
-
View โ It is responsible for displaying all or a portion of the data to the user.
-
Controller โ It is a software Code that controls the interactions between the Model and View.
MVC is popular because it isolates the application logic from the user interface layer and supports separation of concerns. The controller receives all requests for the application and then works with the model to prepare any data needed by the view. The view then uses the data prepared by the controller to generate a final presentable response. The MVC abstraction can be graphically represented as follows-
- Step 1 โ Load framework
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
- Step 2 โ Define AngularJS Application using ng-app directive
<div ng-app = "">...</div>
- **Step 3 โ Define a model name using ng-model directive88
<p>Enter your Name: <input type = "text" ng-model = "name"></p>
- Step 4 โ Bind the value of above model defined using ng-bind directive.
<p>Hello <span ng-bind = "name"></span>!</p>
<html>
<head>
<title>AngularJS First Application</title>
</head>
<body>
<h1>Sample Application</h1>
<div ng-app = "">
<p>Enter your Name: <input type = "text" ng-model = "name"></p>
<p>Hello <span ng-bind = "name"></span>!</p>
</div>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
</body>
</html>
-
ng-app directive indicates the start of AngularJS application.
-
ng-model directive then creates a model variable named "name" which can be used with the html page and within the div having ng-app directive.
-
ng-bind then uses the name model to be displayed in the html span tag whenever user input something in the text box.
-
Closing tag indicates the end of AngularJS application.
- ng-app directive ng-app directive starts an AngularJS Application. It defines the root element. It automatically initializes or bootstraps the application when web page containing AngularJS Application is loaded. It is also used to load various AngularJS modules in AngularJS Application. In following example, we've defined a default AngularJS application using ng-app attribute of a div element.
<div ng-app = "">
...
</div>
- ng-init directive
ng-init directive initializes an AngularJS Application data. It is used to put values to the variables to be used in the application. In following example, we'll initialize an array of countries. We're using JSON syntax to define array of countries.
<div ng-app = "" ng-init = "countries = [{locale:'en-US',name:'United States'}, {locale:'en-GB',name:'United Kingdom'}, {locale:'en-FR',name:'France'}]">
...
</div>