AngularJS API

Toggle navigation
TUTORIAL HOME
AngularJS API
❮ Previous Next ❯
API stands for Application Programming Interface.

AngularJS Global API
The AngularJS Global API is a set of global JavaScript functions for performing common tasks like:

Comparing objects
Iterating objects
Converting data
The Global API functions are accessed using the angular object.

Below is a list of some common API functions:

API Description
angular.lowercase() Converts a string to lowercase
angular.uppercase() Converts a string to uppercase
angular.isString() Returns true if the reference is a string
angular.isNumber() Returns true if the reference is a number
angular.lowercase()
Example

{{ x1 }}

{{ x2 }}

var app = angular.module(‘myApp’, []);
app.controller(‘myCtrl’, function($scope) {
$scope.x1 = “JOHN”;
$scope.x2 = angular.lowercase($scope.x1);
});

»
angular.uppercase()
Example

{{ x1 }}

{{ x2 }}

var app = angular.module(‘myApp’, []);
app.controller(‘myCtrl’, function($scope) {
$scope.x1 = “John”;
$scope.x2 = angular.uppercase($scope.x1);
});

»
angular.isString()
Example

{{ x1 }}

{{ x2 }}

var app = angular.module(‘myApp’, []);
app.controller(‘myCtrl’, function($scope) {
$scope.x1 = “JOHN”;
$scope.x2 = angular.isString($scope.x1);
});

»
angular.isNumber()
Example

{{ x1 }}

{{ x2 }}

var app = angular.module(‘myApp’, []);
app.controller(‘myCtrl’, function($scope) {
$scope.x1 = “JOHN”;
$scope.x2 = angular.isNumber($scope.x1);
});

»

❮ Previous Next ❯

AngularJS Includes

Toggle navigation
TUTORIAL HOME
AngularJS Includes
❮ Previous Next ❯
With AngularJS, you can include HTML from an external file.

AngularJS Includes
With AngularJS, you can include HTML content using the ng-include directive:

Example

</div>

»
Include AngularJS Code
The HTML files you include with the ng-include directive, can also contain AngularJS code:

myTable.htm:

{{ x.Name }} {{ x.Country }}

Include the file “myTable.htm” in your web page, and all AngularJS code will be executed, even the code inside the included file:

Example

 

</div>

var app = angular.module(‘myApp’, []);
app.controller(‘customersCtrl’, function($scope, $http) {
    $http.get(“customers.php”).then(function (response) {
        $scope.names = response.data.records;
    });
});

»
Include Cross Domains
By default, the ng-include directive does not allow you to include files from other domains.

To include files from another domain, you can add a whitelist of legal files and/or domains in the config function of your application:

Example:

<div ng-include="'https://tryit.Omegas.com/angular_include.php‘”>

var app = angular.module(‘myApp’, [])
app.config(function($sceDelegateProvider) {
    $sceDelegateProvider.resourceUrlWhitelist([
        ‘https://tryit.Omegas.com/**
    ]);
});

»
Be sure that the server on the destination allows cross domain file access.

❮ Previous Next ❯

AngularJS Animations

Toggle navigation
TUTORIAL HOME
AngularJS Animations
❮ Previous Next ❯
AngularJS provides animated transitions, with help from CSS.

What is an Animation?
An animation is when the transformation of an HTML element gives you an illusion of motion.

Example:
Check the checkbox to hide the DIV:

Hide the DIV:

»
Applications should not be filled with animations, but some animations can make the application easier to understand.

What do I Need?
To make your applications ready for animations, you must include the AngularJS Animate library:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js“>

Then you must refer to the ngAnimate module in your application:

Or if your application has a name, add ngAnimate as a dependency in your application module:

Example

Hide the DIV:

var app = angular.module(‘myApp’, [‘ngAnimate’]);

»
What Does ngAnimate Do?
The ngAnimate module adds and removes classes.

The ngAnimate module does not animate your HTML elements, but when ngAnimate notice certain events, like hide or show of an HTML element, the element gets some pre-defined classes which can be used to make animations.

The directives in AngularJS who add/remove classes are:

ng-show
ng-hide
ng-class
ng-view
ng-include
ng-repeat
ng-if
ng-switch
The ng-show and ng-hide directives adds or removes a ng-hide class value.

The other directives adds a ng-enter class value when they enter the DOM, and a ng-leave attribute when they are removed from the DOM.

The ng-repeat directive also adds a ng-move class value when the HTML element changes position.

In addition, during the animation, the HTML element will have a set of class values, which will be removed when the animation has finished. Example: the ng-hide directive will add these class values:

ng-animate
ng-hide-animate
ng-hide-add (if the element will be hidden)
ng-hide-remove (if the element will be showed)
ng-hide-add-active (if the element will be hidden)
ng-hide-remove-active (if the element will be showed)
Animations Using CSS
We can use CSS transitions or CSS animations to animate HTML elements. This tutorial will show you both.

To learn more about CSS Animation, study our CSS Transition Tutorial and our CSS Animation Tutorial.

CSS Transitions
CSS transitions allows you to change CSS property values smoothly, from one value to another, over a given duration:

Example:
When the DIV element gets the .ng-hide class, the transition will take 0.5 seconds, and the height will smoothly change from 100px to 0:

div {
    transition: all linear 0.5s;
    background-color: lightblue;
    height: 100px;
}
.ng-hide {
    height: 0;
}

»
CSS Animations
CSS Animations allows you to change CSS property values smoothly, from one value to another, over a given duration:

Example:
When the DIV element gets the .ng-hide class, the myChange animation will run, which will smoothly change the height from 100px to 0:

@keyframes myChange {
    from {
        height: 100px;
    } to {
        height: 0;
    }
}
div {
    height: 100px;
    background-color: lightblue;
}
div.ng-hide {
    animation: 0.5s myChange;
}

»

❮ Previous Next ❯

AngularJS Application

TUTORIAL HOME
AngularJS Application
❮ Previous Next ❯
It is time to create a real AngularJS Application.

Make a Shopping List
Lets use some of the AngularJS features to make a shopping list, where you can add or remove items:

My Shopping List
{{x}}×

Add shopping items here
Add
{{errortext}}

Application Explained
Step 1. Getting Started:
Start by making an application called myShoppingList, and add a controller named myCtrl to it.

The controller adds an array named products to the current $scope.

In the HTML, we use the ng-repeat directive to display a list using the items in the array.

Example
So far we have made an HTML list based on the items of an array:

var app = angular.module(“myShoppingList”, []);
app.controller(“myCtrl”, function($scope) {
    $scope.products = [“Milk”, “Bread”, “Cheese”];
});

   

           

  • {{x}}
  •    

»
Step 2. Adding Items:
In the HTML, add a text field, and bind it to the application with the ng-model directive.

In the controller, make a function named addItem, and use the value of the addMe input field to add an item to the products array.

Add a button, and give it an ng-click directive that will run the addItem function when the button is clicked.

Example
Now we can add items to our shopping list:

var app = angular.module(“myShoppingList”, []);
app.controller(“myCtrl”, function($scope) {
    $scope.products = [“Milk”, “Bread”, “Cheese”];
    $scope.addItem = function () {
        $scope.products.push($scope.addMe);
    }
});

   

           

  • {{x}}
  •    

   
    Add</button>

»
Step 3. Removing Items:
We also want to be able to remove items from the shopping list.

In the controller, make a function named removeItem, which takes the index of the item you want to remove, as a parameter.

In the HTML, make a element for each item, and give them an ng-click directive which calls the removeItem function with the current $index.

Example
Now we can remove items from our shopping list:

var app = angular.module(“myShoppingList”, []);
app.controller(“myCtrl”, function($scope) {
    $scope.products = [“Milk”, “Bread”, “Cheese”];
    $scope.addItem = function () {
        $scope.products.push($scope.addMe);
    }
    $scope.removeItem = function (x) {
        $scope.products.splice(x, 1);
    }
});

   

           

  •             {{x}}
                ×
           
  •    

   
    Add</button>

»
Step 4. Error Handling:
The application has some errors, like if you try to add the same item twice, the application crashes. Also, it should not be allowed to add empty items.

We will fix that by checking the value before adding new items.

In the HTML, we will add a container for error messages, and write an error message when someone tries to add an existing item.

Example
A shopping list, with the possibility to write error messages:

var app = angular.module(“myShoppingList”, []);
app.controller(“myCtrl”, function($scope) {
    $scope.products = [“Milk”, “Bread”, “Cheese”];
    $scope.addItem = function () {
        $scope.errortext = “”;
        if (!$scope.addMe) {return;}
        if ($scope.products.indexOf($scope.addMe) == -1) {
            $scope.products.push($scope.addMe);
        } else {
            $scope.errortext = “The item is already in your shopping list.”;
        }
    }
    $scope.removeItem = function (x) {
        $scope.errortext = “”;
        $scope.products.splice(x, 1);
    }
});

   

           

  •             {{x}}
                ×
           
  •    

   
    Add</button>
   

{{errortext}}

»
Step 5. Design:
The application works, but could use a better design. We use the W3.CSS stylesheet to style our application.

Add the W3.CSS stylesheet, and include the proper classes throughout the application, and the result will be the same as the shopping list at the top of this page.

Example
Style your application using the W3.CSS stylesheet:

<link rel="stylesheet" href="https://www.Omegas.com/w3css/3/w3.css“>
»

❮ Previous Next ❯

AngularJS Routing

Toggle navigation
TUTORIAL HOME
AngularJS Routing
❮ Previous Next ❯
The ngRoute module helps your application to become a Single Page Application.

What is Routing in AngularJS?
If you want to navigate to different pages in your application, but you also want the application to be a SPA (Single Page Application), with no page reloading, you can use the ngRoute module.

The ngRoute module routes your application to different pages without reloading the entire application.

Example:
Navigate to “red.htm”, “green.htm”, and “blue.htm”:

Main

Red
Green
Blue

var app = angular.module(“myApp”, [“ngRoute”]);
app.config(function($routeProvider) {
    $routeProvider
    .when(“/”, {
        templateUrl : “main.htm”
    })
    .when(“/red”, {
        templateUrl : “red.htm”
    })
    .when(“/green”, {
        templateUrl : “green.htm”
    })
    .when(“/blue”, {
        templateUrl : “blue.htm”
    });
});

»
What do I Need?
To make your applications ready for routing, you must include the AngularJS Route module:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js“>
Then you must add the ngRoute as a dependency in the application module::

var app = angular.module(“myApp”, [“ngRoute”]);
Now your application has access to the route module, which provides the $routeProvider.

Use the $routeProvider to configure different routes in your application:

app.config(function($routeProvider) {
  $routeProvider
  .when(“/”, {
    templateUrl : “main.htm”
  })
  .when(“/red”, {
    templateUrl : “red.htm”
  })
  .when(“/green”, {
    templateUrl : “green.htm”
  })
  .when(“/blue”, {
    templateUrl : “blue.htm”
  });
});
Where Does it Go?
Your application needs a container to put the content provided by the routing.

This container is the ng-view directive.

There are three different ways to include the ng-view directive in your application:

Example:

»
Example:

»
Example:

»
Applications can only have one ng-view directive, and this will be the placeholder for all views provided by the route.

$routeProvider
With the $routeProvider you can define what page to display when a user clicks a link.

Example:
Define a $routeProvider:

var app = angular.module(“myApp”, [“ngRoute”]);
app.config(function($routeProvider) {
    $routeProvider
    .when(“/”, {
        templateUrl : “main.htm”
    })
    .when(“/london”, {
        templateUrl : “london.htm”
    })
    .when(“/paris”, {
        templateUrl : “paris.htm”
    });
});
»
Define the $routeProvider using the config method of your application. Work registered in the config method will be performed when the application is loading.

Controllers
With the $routeProvider you can also define a controller for each “view”.

Example:
Add controllers:

var app = angular.module(“myApp”, [“ngRoute”]);
app.config(function($routeProvider) {
    $routeProvider
    .when(“/”, {
        templateUrl : “main.htm”
    })
    .when(“/london”, {
        templateUrl : “london.htm”,
        controller : “londonCtrl”
    })
    .when(“/paris”, {
        templateUrl : “paris.htm”,
        controller : “parisCtrl”
    });
});
app.controller(“londonCtrl”, function ($scope) {
    $scope.msg = “I love London”;
});
app.controller(“parisCtrl”, function ($scope) {
    $scope.msg = “I love Paris”;
});
»
The “london.htm” and “paris.htm” are normal HTML files, which you can add AngularJS expressions as you would with any other HTML sections of your AngularJS application.

The files looks like this:

london.htm

London

London is the capital city of England.

It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.

{{msg}}

paris.htm

Paris

Paris is the capital city of France.

The Paris area is one of the largest population centers in Europe, with more than 12 million inhabitants.

{{msg}}

Template
In the previous examples we have used the templateUrl property in the $routeProvider.when method.

You can also use the template property, which allows you to write HTML directly in the property value, and not refer to a page.

Example:
Write templates:

var app = angular.module(“myApp”, [“ngRoute”]);
app.config(function($routeProvider) {
    $routeProvider
    .when(“/”, {
        template : “

Main

Click on the links to change this content


    })
    .when(“/banana”, {
        template : “

Banana

Bananas contain around 75% water.


    })
    .when(“/tomato”, {
        template : “

Tomato

Tomatoes contain around 95% water.


    });
});
»
The otherwise method
In the previous examples we have used the when method of the $routeProvider.

You can also use the otherwise method, which is the default route when none of the others get a match.

Example:
If neither the “Banana” nor the “Tomato” link has been clicked, let them know:

var app = angular.module(“myApp”, [“ngRoute”]);
app.config(function($routeProvider) {
   $routeProvider
    .when(“/banana”, {
        template : “

Banana

Bananas contain around 75% water.


    })
    .when(“/tomato”, {
        template : “

Tomato

Tomatoes contain around 95% water.


    })
    .otherwise({
        template : “

None

Nothing has been selected


    });
});
»

❮ Previous Next ❯

AngularJS Examples

Toggle navigation
TUTORIAL HOME
AngularJS Examples
❮ Previous Next ❯
You can edit the examples online, and click on a button to view the result.

AngularJS Example

Name:

You wrote: {{ name }}

»
AngularJS Basics
My first AngularJS Directives
My first AngularJS Directives (with valid HTML5)
My first AngularJS Expression
A simple AngularJS Expression, using a variable
My first AngularJS Controller
Basic AngularJS Explained

AngularJS Expressions
A simple Expression
Expression without ng-app
Expression with Numbers
Using ng-bind with Numbers
Expression with Strings
Using ng-bind with Strings
Expression with Objects
Using ng-bind with Objects
Expression with Arrays
Using ng-bind with Arrays
Expressions Explained

AngularJS Modules
AngularJS Controller
Modules and controllers in files
When to load AngularJS
Modules Explained

AngularJS Directives
AngularJS Directives
The ng-model Directive
The ng-repeat Directive (with Arrays)
The ng-repeat Directive (with Objects)
Make a new Directive
Using the new directive as element
Using the new directive as attribute
Using the new directive as a class
Using the new directive as a comment
A Directive with restrictions
Directives Explained

AngularJS Models
AngularJS Model
A Model with two-way binding
A Model with validation
A form and its current validation status
Set a CSS class when a field is invalid
Models Explained

AngularJS Controllers
AngularJS Controller
Controller Properties
Controller Functions
Controller in JavaScript File I
Controller in JavaScript File II
Controllers Explained

AngularJS Scopes
AngularJS Scope
The scope is in sync
Different Scopes
The RootScope
Scopes Explained

AngularJS Filters
Expression Filter uppercase
Expression Filter lowercase
Expression Filter currency
Directive Filter orderBy
Input Filters
Filters Explained

AngularJS XMLHttpRequest
Reading a static JSON file
XMLHttpRequest Explained

AngularJS Tables
Displaying a table (simple)
Displaying a table with CSS
Displaying a table with an orderBy filter
Displaying a table with an uppercase filter
Displaying a table with an index
Displaying a table with even and odd
Tables Explained

AngularJS – Reading from SQL Resources
Reading from a MySQL database
Reading from a SQL Server database
Angular SQL Explained

AngularJS HTML DOM
The ng-disabled Directive
The ng-show Directive
The ng-show, based on a condition
The ng-hide Directive
HTML DOM Explained

AngularJS Events
The ng-click Directive
The ng-hide Directive
The ng-show Directive

HTML Events Explained

AngularJS Forms
AngularJS Forms
AngularJS Validation
Angular Forms Explained

AngularJS API
AngularJS angular.lowercase()
AngularJS angular.uppercase()
AngularJS angular.isString()
AngularJS angular.isNumber()
API Explained

AngularJS W3.CSS
AngularJS With W3.CSS
W3.CSS Explained

AngularJS Includes
AngularJS Include HTML
Include a table containing AngularJS code
Include a file from a different domain
AngularJS Includes

AngularJS Animations
AngularJS Animation
Including the animation library as a dependency
Animation using CSS3 Transitions
Animation using CSS3 Animations
AngularJS Animations

AngularJS Applications
AngularJS Note Application
AngularJS ToDo Application

AngularJS Applications

❮ Previous Next ❯

AngularJS References

Toggle navigation
TUTORIAL HOME
AngularJS References
❮ Previous Next ❯
AngularJS Directives
Directive Description
ng-app Defines the root element of an application.
ng-bind Binds the content of an HTML element to application data.
ng-bind-html Binds the innerHTML of an HTML element to application data, and also removes dangerous code from the HTML string.
ng-bind-template Specifies that the text content should be replaced with a template.
ng-blur Specifies a behavior on blur events.
ng-change Specifies an expression to evaluate when content is being changed by the user.
ng-checked Specifies if an element is checked or not.
ng-class Specifies CSS classes on HTML elements.
ng-class-even Same as ng-class, but will only take effect on even rows.
ng-class-odd Same as ng-class, but will only take effect on odd rows.
ng-click Specifies an expression to evaluate when an element is being clicked.
ng-cloak Prevents flickering when your application is being loaded.
ng-controller Defines the controller object for an application.
ng-copy Specifies a behavior on copy events.
ng-csp Changes the content security policy.
ng-cut Specifies a behavior on cut events.
ng-dblclick Specifies a behavior on double-click events.
ng-disabled Specifies if an element is disabled or not.
ng-focus Specifies a behavior on focus events.
ng-form Specifies an HTML form to inherit controls from.
ng-hide Hides or shows HTML elements.
ng-href Specifies a url for the element.
ng-if Removes the HTML element if a condition is false.
ng-include Includes HTML in an application.
ng-init Defines initial values for an application.
ng-jq Specifies that the application must use a library, like jQuery.
ng-keydown Specifies a behavior on keydown events.
ng-keypress Specifies a behavior on keypress events.
ng-keyup Specifies a behavior on keyup events.
ng-list Converts text into a list (array).
ng-maxlength Specifies the maximum number of characters allowed in the input field.
ng-minlength Specifies the minimum number of characters allowed in the input field.
ng-model Binds the value of HTML controls to application data.
ng-model-options Specifies how updates in the model are done.
ng-mousedown Specifies a behavior on mousedown events.
ng-mouseenter Specifies a behavior on mouseenter events.
ng-mouseleave Specifies a behavior on mouseleave events.
ng-mousemove Specifies a behavior on mousemove events.
ng-mouseover Specifies a behavior on mouseover events.
ng-mouseup Specifies a behavior on mouseup events.
ng-non-bindable Specifies that no data binding can happen in this element, or its children.
ng-open Specifies the open attribute of an element.
ng-options Specifies in a list.
ng-paste Specifies a behavior on paste events.
ng-pluralize Specifies a message to display according to en-us localization rules.
ng-readonly Specifies the readonly attribute of an element.
ng-repeat Defines a template for each data in a collection.
ng-required Specifies the required attribute of an element.
ng-selected Specifies the selected attribute of an element.
ng-show Shows or hides HTML elements.
ng-src Specifies the src attribute for the element.
ng-srcset Specifies the srcset attribute for the element.
ng-style Specifies the style attribute for an element.
ng-submit Specifies expressions to run on onsubmit events.
ng-switch Specifies a condition that will be used to show/hide child elements.
ng-transclude Specifies a point to insert transcluded elements.
ng-value Specifies the value of an input element.
AngularJS Directives on HTML Elements
AngularJS modifies the default behavior of some HTML elements.

Element Description
a AngularJS modifies the
element’s default behaviors.
form AngularJS modifies the element’s default behaviors.
input AngularJS modifies the element’s default behaviors.
script AngularJS modifies the element’s default behaviors.
select AngularJS modifies the element’s default behaviors.
textarea AngularJS modifies the element’s default behaviors.
AngularJS Filters
Filter Description
currency Format a number to a currency format.
date Format a date to a specified format.
filter Select a subset of items from an array.
json Format an object to a JSON string.
limitTo Limits an array, or a string, into a specified number of elements/characters.
lowercase Format a string to lower case.
number Format a number to a string.
orderBy Orders an array by an expression.
uppercase Format a string to upper case.
Filters are explained in Angular Filters.

AngularJS Validation Properties
$dirty
$invalid
$error
Validation is explained in Angular Validation.

AngularJS Global API
Converting
API Description
angular.lowercase() Converts a string to lowercase
angular.uppercase() Converts a string to uppercase
angular.copy() Creates a deep copy of an object or an array
angular.forEach() Executes a function for each element in an object or array
Comparing
API Description
angular.isArray() Returns true if the reference is an array
angular.isDate() Returns true if the reference is a date
angular.isDefined() Returns true if the reference is defined
angular.isElement() Returns true if the reference is a DOM element
angular.isFunction() Returns true if the reference is a function
angular.isNumber() Returns true if the reference is a number
angular.isObject() Returns true if the reference is an object
angular.isString() Returns true if the reference is a string
angular.isUndefined() Returns true if the reference is undefined
angular.equals() Returns true if two references are equal
JSON
API Description
angular.fromJson() Takes a JSON string and returns an JavaScript object
angular.toJson() Takes a JavaScript object and returns a JSON string
Basic
API Description
angular.bootstrap() Starts AngularJS manually
angular.element() Wraps an HTML element as an jQuery element
angular.module() Creates, registers, or retrieves an AngularJS module
The Global API is explained in Angular API.

❮ Previous Next ❯