YouTip LogoYouTip

Angularjs Intro

* * * AngularJS is a **JavaScript framework**. It can be added to an HTML page using the `` tag. AngularJS extends HTML through **(#)** and binds data to HTML using **(#)**. * * * ## AngularJS is a JavaScript Framework AngularJS is a JavaScript framework. It is a library written in JavaScript. AngularJS is distributed as a JavaScript file and can be added to a web page using the script tag: | ![Image 1: Note](#) | We recommend placing the script at the bottom of the `` element. This improves web page loading speed because HTML loading is not blocked by script loading. | | --- | Download various angular.js versions: [https://github.com/angular/angular.js/releases](https://github.com/angular/angular.js/releases) * * * ## AngularJS Extends HTML AngularJS extends HTML using **ng-directives**. The **ng-app** directive defines an AngularJS application. The **ng-model** directive binds the value of an HTML element (e.g., an input field) to an application variable. The **ng-bind** directive binds application data to the HTML view. ## AngularJS Example

Name:

Hello {{name}}

[Try it Β»](#) Explanation of the example: When the web page loads, AngularJS starts automatically. The **ng-app** directive tells AngularJS that the `
` element is the "owner" of the AngularJS **application**. The **ng-model** directive binds the value of the input field to the application variable **name**. The **ng-bind** directive binds the application variable `name` to the innerHTML of a certain paragraph. | ![Image 2: Note](#) | If you remove the **ng-app** directive, HTML will display the expression directly without evaluating its result. | | --- | * * * ## What is AngularJS? AngularJS makes it easier to develop modern Single Page Applications (SPAs). * AngularJS binds application data to HTML elements. * AngularJS can clone and repeat HTML elements. * AngularJS can hide and show HTML elements. * AngularJS can add code "behind" HTML elements. * AngularJS supports input validation. * * * ## AngularJS Directives As you can see, AngularJS directives are HTML attributes prefixed with **ng**. The **ng-init** directive initializes AngularJS application variables. ## AngularJS Example

Name is

[Try it Β»](#) | ![Image 3: Note](#) | HTML5 allows custom attributes prefixed with **data-**. AngularJS attributes are prefixed with **ng-**, but you can use **data-ng-** to make the page valid for HTML5. | | --- | Valid HTML5: ## AngularJS Example

Name is

[Try it Β»](#) * * * ## AngularJS Expressions AngularJS expressions are written inside double curly braces: **{{ expression }}**. AngularJS expressions bind data to HTML, similar to the **ng-bind** directive. AngularJS will "output" the data where the expression is written. **AngularJS expressions** are very similar to **JavaScript expressions**: they can contain literals, operators, and variables. Example: `{{ 5 + 5 }}` or `{{ firstName + " " + lastName }}` ## AngularJS Example

My first expression: {{ 5 + 5 }}

[Try it Β»](#) * * * ## AngularJS Application AngularJS **modules** define AngularJS applications. AngularJS **controllers** control AngularJS applications. The **ng-app** directive defines the application, and the **ng-controller** directive defines the controller. ## AngularJS Example
First Name:
Last Name:

Full Name: {{firstName + " " + lastName}}
var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.firstName= "John"; $scope.lastName= "Doe"; }); [Try it Β»](#) The AngularJS module defines the application: ## AngularJS Module var app = angular.module('myApp', []); The AngularJS controller controls the application: ## AngularJS Controller app.controller('myCtrl', function($scope){ $scope.firstName= "John"; $scope.lastName= "Doe"; }); In the following tutorials, you will learn more about applications and modules.
← Angularjs ExpressionsAngularjs Tutorial β†’