AngularJS W3schhols

download AngularJS W3schhols

of 180

Transcript of AngularJS W3schhols

  • 7/24/2019 AngularJS W3schhols

    1/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    TIP

    PressCTRL+B orCLICK Bookmarks in your PDF reader for easy navigation

  • 7/24/2019 AngularJS W3schhols

    2/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS extends HTML with new attributes.

    AngularJS is perfect for Single Page Applications (SPAs).

    AngularJS is easy to learn.

    This tutorial is specially designed to help you learn AngularJS as quickly and

    efficiently as possible.

    First, you will learn the basics of AngularJS: directives, expressions, filters,modules, and controllers.

    Then you will learn everything else you need to know about AngularJS:

    Events, DOM, Forms, Input, Validation, Http, and more.

  • 7/24/2019 AngularJS W3schhols

    3/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS Example

    Name :

    Hello {{name}}

    What You Should Already Know

    Before you study AngularJS, you should have a basic understanding of:

    HTML

    CSS

    JavaScript

    AngularJS History

    AngularJS version 1.0 was released in 2012.

    Miko 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.

  • 7/24/2019 AngularJS W3schhols

    4/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS is a JavaScript framework. It can be added to an HTML page with a tag.

    AngularJS extends HTML attributes with Directives, and binds data to HTMLwithExpressions.

    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

    with a script tag:

    AngularJS extends HTML with ng-directives.

    The ng-appdirective defines an AngularJS application.

    The ng-modeldirective binds the value of HTML controls (input, select,

    textarea) to application data.

    The ng-binddirective binds application data to the HTML view.

    AngularJS Example

    Name:

  • 7/24/2019 AngularJS W3schhols

    5/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Example explained:

    AngularJS starts automatically when the web page has loaded.

    The ng-appdirective tells AngularJS that the element is the "owner" of

    an AngularJSapplication.

    The ng-modeldirective binds the value of the input field to the application

    variable name.

    The ng-binddirective binds the innerHTMLof the

    element to the

    application variablename.

    As you have already seen, AngularJS directives are HTML attributes with

    an ngprefix.

    The ng-initdirective initialize AngularJS application variables.

    AngularJS Example

    The name is

    Alternatively with valid HTML:

    AngularJS Example

    The name is

    You can use data-ng-, instead of ng-, if you want to make your page

    HTML valid.

    You will learn a lot more about directives later in this tutorial.

  • 7/24/2019 AngularJS W3schhols

    6/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS expressions are written inside double braces: {{ expression }}.

    AngularJS will "output" data exactly where the expression is written:

    AngularJS Example

    My first expression: {{ 5 + 5 }}

    AngularJS expressions bind AngularJS data to HTML the same way as the ng-

    binddirective.

    AngularJS Example

    Name:

    {{name}}

    You will learn more about expressions later in this tutorial.

  • 7/24/2019 AngularJS W3schhols

    7/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS modulesdefine AngularJS applications.

    AngularJS controllerscontrol AngularJS applications.

    The ng-appdirective defines the application, the ng-controllerdirective

    defines the controller.

    AngularJS ExampleFirst Name:
    Last Name:

    Full Name: {{firstName + " " + lastName}}var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.firstName= "John";$scope.lastName= "Doe";

    });

    AngularJS modules define applications:

    var app = angular.module('myApp', []);

    AngularJS controllers control applications:

    app.controller('myCtrl', function($scope) {

    $scope.firstName= "John";

    $scope.lastName= "Doe";

    });

  • 7/24/2019 AngularJS W3schhols

    8/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS binds data to HTML using Expressions.

    AngularJS expressions can be written inside double

    braces: {{ expression}}.

    AngularJS expressions can also be written inside a directive: ng-bind="expression".

    AngularJS will resolve the expression, and return the result exactly where the

    expression is written.

    AngularJS expressionsare much like JavaScript expressions:They can

    contain literals, operators, and variables.

    Example {{ 5 + 5 }} or {{ firstName + " " + lastName }}

    Example

    My first expression: {{ 5 + 5 }}

  • 7/24/2019 AngularJS W3schhols

    9/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    If you remove the ng-appdirective, HTML will display the expression as it is,

    without solving it:

    Example

    My first expression: {{ 5 + 5 }}

    You can write expressions wherever you like, AngularJS will simply resolve the

    expression and return the result.

    Example: Let AngularJS change the value of CSS properties.

    Change the color of the input box below, by changing it's value:

    lightblue

    Example

    AngularJS numbers are like JavaScript numbers:

    Example

    Total in dollar: {{ quantity * cost }}

  • 7/24/2019 AngularJS W3schhols

    10/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Same example using ng-bind:

    Example

    Total in dollar:

    Using ng-initis not very common. You will learn a better way to

    initialize data in the chapter about controllers.

    AngularJS strings are like JavaScript strings:

    Example

    The name is {{ firstName + " " + lastName }}

    Same example using ng-bind:

    Example

    The name is

  • 7/24/2019 AngularJS W3schhols

    11/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS objects are like JavaScript objects:

    Example

    The name is {{ person.lastName }}

    Same example using ng-bind:

    Example

    The name is

    AngularJS arrays are like JavaScript arrays:

    Example

    The third result is {{ points[2] }}

    Same example using ng-bind:

    Example

    The third result is

  • 7/24/2019 AngularJS W3schhols

    12/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Like JavaScript expressions, AngularJS expressions can contain literals,

    operators, and variables.

    Unlike JavaScript expressions, AngularJS expressions can be written inside

    HTML.

    AngularJS expressions do not support conditionals, loops, and exceptions, while

    JavaScript expressions do.

    AngularJS expressions support filters, while JavaScript expressions do not.

  • 7/24/2019 AngularJS W3schhols

    13/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    An AngularJS module defines an application.

    The module is a container for the different parts of an application.

    The module is a container for the application controllers.

    Controllers always belong to a module.

    A module is created by using the AngularJS function angular.module

    ...

    varapp = angular.module("myApp", []);

    The "myApp" parameter refers to an HTML element in which the application will

    run.

    Now you can add controllers, directives, filters, and more, to your AngularJS

    application.

  • 7/24/2019 AngularJS W3schhols

    14/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Add a controller to your application, and refer to the controller with the ng-

    controllerdirective:

    Example{{ firstName + " " + lastName }}

    varapp = angular.module("myApp", []);

    app.controller("myCtrl", function($scope) {

    $scope.firstName = "John";$scope.lastName = "Doe";});

    You will learn more about controllers later in this tutorial.

    AngularJS has a set of built-in directives which you can use to add functionality

    to your application.

    In addition you can use the module to add your own directives to your

    applications:

    Examplevarapp = angular.module("myApp", []);app.directive("w3TestDirective", function() {

    return{template : "I was made in a directive constructor!"

    };});

  • 7/24/2019 AngularJS W3schhols

    15/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    It is common in AngularJS applications to put the module and the controllers in

    JavaScript files.

    In this example, "myApp.js" contains an application module definition, while

    "myCtrl.js" contains the controller:

    Example

    {{ firstName + " " + lastName }}

    myApp.js

    var app = angular.module("myApp", []);

    The [] parameter in the module definition can be used to define

    dependent modules.

    Without the [] parameter, you are not creatinga new module,

    but retrievingan existing one.

  • 7/24/2019 AngularJS W3schhols

    16/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    myCtrl.js

    app.controller("myCtrl", function($scope) {

    $scope.firstName = "John";

    $scope.lastName= "Doe";

    });

    Global functions should be avoided in JavaScript. They can easily be overwritten

    or destroyed by other scripts.

    AngularJS modules reduces this problem, by keeping all functions local to the

    module.

    While it is common in HTML applications to place scripts at the end of

    theelement, it is recommended that you load the AngularJS libraryeither in theor at the start of the.

    This is because calls to angular.modulecan only be compiled after the library

    has been loaded.

    Example{{ firstName + " " + lastName }}

    var app = angular.module("myApp", []);app.controller("myCtrl", function($scope) {$scope.firstName = "John";$scope.lastName = "Doe";

    });

  • 7/24/2019 AngularJS W3schhols

    17/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS lets you extend HTML with new attributes called Directives.

    AngularJS has a set of built-in directives which offers functionality to yourapplications.

    AngularJS also lets you define your own directives.

    AngularJS directives are extended HTML attributes with the prefix ng-.

    The ng-appdirective initializes an AngularJS application.

    The ng-initdirective initializes application data.

    The ng-modeldirective binds the value of HTML controls (input, select,

    textarea) to application data.

    Read about all AngularJS directives in ourAngularJS directive reference.

    Example

    Name:

    You wrote: {{ firstName }}

    The ng-appdirective also tells AngularJS that the element is the "owner"

    of the AngularJS application.

    http://www.w3schools.com/angular/angular_ref_directives.asphttp://www.w3schools.com/angular/angular_ref_directives.asphttp://www.w3schools.com/angular/angular_ref_directives.asphttp://www.w3schools.com/angular/angular_ref_directives.asp
  • 7/24/2019 AngularJS W3schhols

    18/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The {{ firstName }}expression, in the example above, is an AngularJS data

    binding expression.

    Data binding in AngularJS binds AngularJS expressions with AngularJS data.

    {{ firstName }}is bound with ng-model="firstName".

    In the next example two text fields are bound together with two ng-model

    directives:

    Example

    Quantity: Costs:

    Total in dollar: {{ quantity * price }}

    Using ng-initis not very common. You will learn how to initialize

    data in the chapter about controllers.

    The ng-repeatdirective repeats an HTML element:

    Example

    {{ x }}

  • 7/24/2019 AngularJS W3schhols

    19/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-repeatdirective actually clones HTML elementsonce for each item in

    a collection.

    The ng-repeatdirective used on an array of objects:

    Example

    {{ x.name + ', ' + x.country }}

    AngularJS is perfect for database CRUD (Create Read Update

    Delete) applications.

    Just imagine if these objects were records from a database.

    The ng-appdirective defines the root elementof an AngularJS application.

    The ng-appdirective will auto-bootstrap(automatically initialize) the

    application when a web page is loaded.

  • 7/24/2019 AngularJS W3schhols

    20/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-initdirective defines initial valuesfor an AngularJS application.

    Normally, you will not use ng-init. You will use a controller or module instead.

    You will learn more about controllers and modules later.

    The ng-modeldirective binds the value of HTML controls (input, select,

    textarea) to application data.

    The ng-modeldirective can also:

    Provide type validation for application data (number, email, required).

    Provide status for application data (invalid, dirty, touched, error). Provide CSS classes for HTML elements.

    Bind HTML elements to HTML forms.

    Read more about the ng-modeldirective in the next chapter.

  • 7/24/2019 AngularJS W3schhols

    21/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    In addition to all the built-in AngularJS directives, you can create your own

    directives.

    New directives are created by using the .directivefunction.

    To invoke the new directive, make an HTML element with the same tag name as

    the new directive.

    When naming a directive, you must use a camel case name,w3TestDirective,

    but when invoking it, you must use -separated name,w3-test-directive:

    Example

    var app = angular.module("myApp", []);app.directive("w3TestDirective", function() {

    return {template : "Made by a directive!"

    };

    });

    You can invoke a directive by using:

    Element name

    Attribute

    Class

    Comment

    The examples below will all produce the same result:

    Element name

  • 7/24/2019 AngularJS W3schhols

    22/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Attribute

    Class

    Comment

    You can restrict your directives to only be invoked by some of the methods.

    ExampleBy adding arestrictproperty with the value"A", the directive can only be invokedby attributes:varapp = angular.module("myApp", []);app.directive("w3TestDirective", function() {

    return{restrict : "A",template : "Made by a directive!"

    };

    });

    The legal restrict values are:

    Efor Element name

    Afor Attribute

    Cfor Class

    Mfor Comment

    By default the value is EA, meaning that both Element names and attributenames can invoke the directive.

  • 7/24/2019 AngularJS W3schhols

    23/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-model directive binds the value of HTML controls (input, select,textarea) to application data.

    With the ng-modeldirective you can bind the value of an input field to a

    variable created in AngularJS.

    Example

    Name:

    var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.name = "John Doe";});

    The binding goes both ways. If the user changes the value inside the input field,

    the AngularJS property will also change it's value:

    Example

    Name: You entered: {{name}}

  • 7/24/2019 AngularJS W3schhols

    24/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-modeldirective can provide type validation for application data

    (number, e-mail, required):

    Example

    Email:Not a valid e-mail

    address

    In the example above, the span will be displayed only if the expression in

    the ng-showattribute returns true.

    If the property in the ng-modelattribute does not exist, AngularJS

    will create one for you.

    The ng-modeldirective can provide status for application data (invalid, dirty,

    touched, error):

    Example

    Email:

    Status{{myForm.myAddress.$valid}}{{myForm.myAddress.$dirty}}{{myForm.myAddress.$touched}}

  • 7/24/2019 AngularJS W3schhols

    25/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-modeldirective provides CSS classes for HTML elements, depending on

    their status:

    Exampleinput.ng-invalid {

    background-color:lightblue;}

    Enter your name:

    The ng-modeldirective adds/removes the following classes, according to the

    status of the form field:

    ng-empty

    ng-not-empty

    ng-touched

    ng-untouched

    ng-valid

    ng-invalid

    ng-dirty

    ng-pending

    ng-pristine

  • 7/24/2019 AngularJS W3schhols

    26/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS controllers control the dataof AngularJS applications.

    AngularJS controllers are regular JavaScript Objects.

    AngularJS applications are controlled by controllers.

    The ng-controllerdirective defines the application controller.

    A controller is a JavaScript Object, created by a standard JavaScript object

    constructor.

    AngularJS ExampleFirst Name:
    Last Name:

    Full Name: {{firstName + " " + lastName}}var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.firstName = "John";$scope.lastName = "Doe";

    });

    Application explained:

    The AngularJS application is defined by ng-app="myApp". The application

    runs inside the .

    The ng-controller="myCtrl"attribute is an AngularJS directive. It defines a

    controller.

  • 7/24/2019 AngularJS W3schhols

    27/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The myCtrlfunction is a JavaScript function.

    AngularJS will invoke the controller with a $scopeobject.

    In AngularJS, $scope is the application object (the owner of application

    variables and functions).

    The controller creates two properties (variables) in the scope

    (firstNameand lastName).

    The ng-modeldirectives bind the input fields to the controller properties

    (firstName and lastName).

    The example above demonstrated a controller object with two properties:

    lastName and firstName.

    A controller can also have methods (variables as functions):

    AngularJS Example

    First Name:
    Last Name:

    Full Name: {{fullName()}}

    var app = angular.module('myApp', []);app.controller('personCtrl', function($scope) {

    $scope.firstName = "John";$scope.lastName = "Doe";

    $scope.fullName = function() {return $scope.firstName + " " + $scope.lastName;

    };});

  • 7/24/2019 AngularJS W3schhols

    28/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    In larger applications, it is common to store controllers in external files.

    Just copy the code between the tags into an external file

    namedpersonController.js:

    AngularJS Example

    First Name:
    Last Name:

    Full Name: {{firstName + " " + lastName}}

    For the next example we will create a new controller file:

    angular.module('myApp', []).controller('namesCtrl', function($scope) {

    $scope.names = [

    {name:'Jani',country:'Norway'},

    {name:'Hege',country:'Sweden'},

    {name:'Kai',country:'Denmark'}

    ];

    });

    Save the file as namesController.js:

    And then use the controller file in an application:

    http://www.w3schools.com/angular/personController.jshttp://www.w3schools.com/angular/personController.jshttp://www.w3schools.com/angular/namesController.jshttp://www.w3schools.com/angular/namesController.jshttp://www.w3schools.com/angular/personController.js
  • 7/24/2019 AngularJS W3schhols

    29/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS Example

    {{ x.name + ', ' + x.country }}

  • 7/24/2019 AngularJS W3schhols

    30/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The scope is the binding part between the HTML (view) and the JavaScript(controller).

    The scope is an object with the available properties and methods.

    The scope is available for both the view and the controller.

    When you make a controller in AngularJS, you pass the $scopeobject as an

    argument:

    ExampleProperties made in the controller, can be referred to in the view:{{carname}}

    varapp = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.carname = "Volvo";});

    When adding properties to the $scope object in the controller, the view (HTML)

    gets access to these properties.

    In the view, you do not use the prefix $scope, you just refer to a

    propertyname, like{{carname}}.

  • 7/24/2019 AngularJS W3schhols

    31/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    If we consider an AngularJS application to consist of:

    View, which is the HTML.

    Model, which is the data available for the current view.

    Controller, which is the JavaScript function that

    makes/changes/removes/controls the data.

    Then the scope is the Model.

    The scope is a JavaScript object with properties and methods, which are

    available for both the view and the controller.

    ExampleIf you make changes in the view, the model and the controller will be updated:My name is {{name}}varapp = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.name = "John Dow";

    });

    It is important to know which scope you are dealing with, at any time.

    In the two examples above there is only one scope, so knowing your scope is

    not an issue, but for larger applications there can be sections in the HTML DOM

    which can only access certain scopes.

  • 7/24/2019 AngularJS W3schhols

    32/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Example

    When dealing with the ng-repeatdirective, each repetition has access to thecurrent repetition object:

    {{x}}varapp = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.names = ["Emil", "Tobias", "Linus"];});

    Eachelement has access to the current repetition object, in this case a

    string, which is referred to by using x.

    All applications have a $rootScopewhich is the scope created on the HTML

    element that contains the ng-appdirective.

    The rootScope is available in the entire application.

    If a variable has the same name in both the current scope and in the rootScope,

    the application use the one in the current scope.

    ExampleA variable named "color" exists in both the controller's scope and in therootScope:

    The rootScope's favorite color:

    {{color}}

    The scope of the controller's favorite color:

    {{color}}

    The rootScope's favorite color is still:

    {{color}}

    varapp = angular.module('myApp', []);app.run(function($rootScope) {

    $rootScope.color = 'blue';});app.controller('myCtrl', function($scope) {

    $scope.color = "red";});

  • 7/24/2019 AngularJS W3schhols

    33/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Filters can be added in AngularJS to format data.

    AngularJS provides filters to transform data:

    currencyFormat a number to a currency format.

    dateFormat a date to a specified format. filterSelect a subset of items from an array.

    jsonFormat an object to a JSON string.

    limitToLimits an array/string, into a specified number of

    elements/characters.

    lowercaseFormat a string to lower case.

    numberFormat a number to a string.

    orderByOrders an array by an expression.

    uppercaseFormat a string to upper case.

    Filters can be added to expressions by using the pipe character |, followed by a

    filter.

    The uppercasefilter format strings to upper case:

    Example

    The name is {{ lastName | uppercase }}

  • 7/24/2019 AngularJS W3schhols

    34/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The lowercasefilter format strings to lower case:

    Example

    The name is {{ lastName | lowercase }}

    Filters are added to directives, like ng-repeat, by using the pipe character |,

    followed by a filter:

    ExampleThe orderByfilter sorts an array:

    {{ x.name + ', ' + x.country }}

    The currencyfilter formats a number as currency:

    Example

    Price: {{ price | currency }}

  • 7/24/2019 AngularJS W3schhols

    35/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The filterfilter selects a subset of an array.

    The filterfilter can only be used on arrays, and it returns an array containing

    only the matching items.

    ExampleReturn the names that contains the letter "i":

    {{ x }}

    By setting the ng-modeldirective on an input field, we can use the value of the

    input field as an expression in a filter.

    Type a letter in the input field, and the list will shrink/grow depending on the

    match:

    Jani

    Carl

    Margareth

    Hege

    Joe

    Gustav

    Birgit

    Mary

    Kai

  • 7/24/2019 AngularJS W3schhols

    36/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Example

    {{ x }}

    Click the table headers to change the sort order::

    Name Country

    Jani Norway

    Carl Sweden

    Margareth England

    Hege Norway

    Joe Denmark

    Gustav Sweden

    Birgit Denmark

    Mary England

    Kai Norway

  • 7/24/2019 AngularJS W3schhols

    37/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    By adding the ng-clickdirective on the table headers, we can run a function

    that changes the sorting order of the array:

    Example

    NameCountry

    {{x.name}}{{x.country}}

    angular.module('myApp', []).controller('namesCtrl', function($scope) {

    $scope.names = [{name:'Jani',country:'Norway'},{name:'Carl',country:'Sweden'},{name:'Margareth',country:'England'},{name:'Hege',country:'Norway'},{name:'Joe',country:'Denmark'},{name:'Gustav',country:'Sweden'},

    {name:'Birgit',country:'Denmark'},{name:'Mary',country:'England'},{name:'Kai',country:'Norway'}

    ];$scope.orderByMe = function(x) {$scope.myOrderBy = x;

    }});

  • 7/24/2019 AngularJS W3schhols

    38/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    You can make your own filters by register a new filter factory function with your

    module:

    ExampleMake a custom filter called "myFormat":

    {{x | myFormat}}

    varapp = angular.module('myApp', []);app.filter('myFormat', function() {

    returnfunction(x) {vari, c, txt = "";x = x.split("")for(i = 0; i < x.length; i++) {

    c = x[i];if(i % 2== 0) {

    c = c.toUpperCase();}txt += c;

    }returntxt;};

    });app.controller('namesCtrl', function($scope) {

    $scope.names =['Jani', 'Carl', 'Margareth', 'Hege', 'Joe', 'Gustav','Birgit', 'Mary', 'Kai'];});

  • 7/24/2019 AngularJS W3schhols

    39/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    In AngularJS you can make your own service, or use one of the many built-in services.

    In AngularJS, a service is a function, or object, that is available for, and limited

    to, your AngularJS application.

    AngularJS has about 30 built-in services. One of them is the $locationservice.

    The $locationservice has methods which return information about the

    location of the current web page:

    Example

    Use the $locationservice in a controller:

    varapp = angular.module('myApp', []);

    app.controller('customersCtrl', function($scope, $location) {myUrl = $location.absUrl();

    });

    Note that the $locationservice is passed in to the controller as an argument.

    In order to use the service in the controller, it must be defined as a

    dependency.

    For many services, like the $locationservice, it seems like you could use

    objects that are already in the DOM, like thewindow.locationobject, and you

    could, but it would have some limitations, at least for your AngularJS

    application.

  • 7/24/2019 AngularJS W3schhols

    40/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS constantly supervise your application, and for it to handle changes

    and events properly, AngularJS prefers that you use the $locationservice

    instead of thewindow.location object.

    The $httpservice is one of the most common used services in AngularJS

    applications. The service makes a request to the server, and lets your

    application handle the response.

    Example

    Use the $httpservice to request data from the server:varapp = angular.module('myApp', []);app.controller('myCtrl', function($scope, $http) {

    $http.get("welcome.htm").then(function(response) {$scope.myWelcome = response.data;

    });});

    This example demonstrates a very simple use of the $httpservice.

    The $timeout service is AngularJS' version of thewindow.setTimeout

    function.

    Example

    Display a new message after two seconds:varapp = angular.module('myApp', []);app.controller('myCtrl', function($scope, $timeout) {

    $scope.myHeader = "Hello World!";$timeout(function() {

    $scope.myHeader = "How are you today?";}, 2000);

    });

  • 7/24/2019 AngularJS W3schhols

    41/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The $intervalservice is AngularJS' version of thewindow.setInterval

    function.

    Example

    Display the time every second:

    varapp = angular.module('myApp', []);app.controller('myCtrl', function($scope, $interval) {

    $scope.theTime = newDate().toLocaleTimeString();$interval(function() {

    $scope.theTime = newDate().toLocaleTimeString();}, 1000);

    });

    To create your own service, connect your service to the module:

    Create a service named hexafy:

    app.service('hexafy', function() {

    this.myFunc = function(x) {

    returnx.toString(16);

    }

    });

    To use your custom made service, add it as a dependency when defining the

    filter:

    ExampleUse the custom made service named hexafyto convert a number into a

    hexadecimal number:app.controller('myCtrl', function($scope, hexafy) {

    $scope.hex = hexafy.myFunc(255);});

  • 7/24/2019 AngularJS W3schhols

    42/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Once you have created a service, and connected it to your application, you can

    use the service in any controller, directive, filter, or even inside other services.

    To use the service inside a filter, add it as a dependency when defining the

    filter:

    The service hexafyused in the filtermyFormat:

    app.filter('myFormat',['hexify', function(hexify) {

    returnfunction(x) {

    returnhexify.myFunc(x);

    };}]);

    You can use the filter when displaying values from an object, or an array:

    Create a service named hexafy:

    {{x | myFormat}}

  • 7/24/2019 AngularJS W3schhols

    43/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    $httpis an AngularJS service for reading data from remote servers.

    The AngularJS $httpservice makes a request to the server, and returns a

    response.

    ExampleMake a simple request to the server, and display the result in a header:

    Today's welcome message is:

    {{myWelcome}}varapp = angular.module('myApp', []);app.controller('myCtrl', function($scope, $http) {

    $http.get("welcome.htm").then(function(response) {

    $scope.myWelcome = response.data;

    });});

    The example above uses the .getmethod of the $httpservice.

    The .get method is a shortcut method of the $http service. There are several

    shortcut methods:

    .delete() .get()

    .head()

    .jsonp()

    .patch()

    .post()

    .put()

  • 7/24/2019 AngularJS W3schhols

    44/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The methods above are all shortcuts of calling the $http service:

    Examplevarapp = angular.module('myApp', []);

    app.controller('myCtrl', function($scope, $http) {$http({method : "GET",url : "welcome.htm"

    }).then(functionmySucces(response) {$scope.myWelcome = response.data;

    }, functionmyError(response) {$scope.myWelcome = response.statusText;

    });});

    The example above executes the $http service with one argument, an object

    specifying the HTTP method, the url, what to do on success, and what to to on

    failure.

    The response from the server is an object with these properties:

    .configthe object used to generate the request.

    .dataa string, or an object, carrying the response from the server.

    .headersa function to use to get header information.

    .statusa number defining the HTTP status.

    .statusTexta string defining the HTTP status.

    Examplevarapp = angular.module('myApp', []);app.controller('myCtrl', function($scope, $http) {

    $http.get("welcome.htm").then(function(response) {

    $scope.content = response.data;$scope.statuscode = response.status;$scope.statustext = response.statustext;

    });});

    To handle errors, add one more function to the .thenmethod:

  • 7/24/2019 AngularJS W3schhols

    45/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Examplevarapp = angular.module('myApp', []);app.controller('myCtrl', function($scope, $http) {

    $http.get("wrongfilename.htm").then(function(response) {

    //First function handles success$scope.content = response.data;

    }, function(response) {//Second function handles error$scope.content = "Something went wrong";

    });});

    The data you get from the response is expected to be in JSON format.

    JSON is a great way of transporting data, and it is easy to use within AngularJS,

    or any other JavaScript.

    Example: On the server we have a file that returns a JSON object containing 15

    customers, all wrapped in array called records.

    ExampleThe ng-repeatdirective is perfect for looping through an array:

    {{ x.Name + ', ' + x.Country }}

    varapp = angular.module('myApp', []);app.controller('customersCtrl', function($scope, $http) {

    $http.get("customers.php").then(function(response) {$scope.myData = response.data.records;

    });});

  • 7/24/2019 AngularJS W3schhols

    46/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Application explained:

    The application defines the customersCtrlcontroller, with

    a $scopeand $httpobject.

    $httpis an XMLHttpRequest objectfor requesting external data.

    $http.get()reads JSON

    datafromhttp://www.w3schools.com/angular/customers.php.

    On success, the controller creates a property,myData, in the scope, with JSON

    data from the server.

    http://www.w3schools.com/angular/customers.phphttp://www.w3schools.com/angular/customers.phphttp://www.w3schools.com/angular/customers.phphttp://www.w3schools.com/angular/customers.php
  • 7/24/2019 AngularJS W3schhols

    47/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-repeat directive is perfect for displaying tables.

    Displaying tables with angular is very simple:

    AngularJS Example

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

    var app = angular.module('myApp', []);app.controller('customersCtrl', function($scope, $http) {

    $http.get("http://www.w3schools.com/angular/customers.php").then(function (response) {$scope.names = response.data.records;});

    });

    To make it nice, add some CSS to the page:

    CSS Styletable, th , td {

    border:1px solid grey;

    border-collapse:collapse;padding:5px;}table tr:nth-child(odd) {

    background-color:#f1f1f1;}table tr:nth-child(even) {

    background-color:#ffffff;}

  • 7/24/2019 AngularJS W3schhols

    48/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    To sort the table, add an orderByfilter:

    AngularJS Example

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

    To display uppercase, add an uppercasefilter:

    AngularJS Example

    {{ x.Name }}{{ x.Country | uppercase }}

    To display the table index, add a with $index:

    AngularJS Example

    {{ $index + 1 }}{{ x.Name }}{{ x.Country }}

  • 7/24/2019 AngularJS W3schhols

    49/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS Example{{ x.Name }}{{ x.Name }}{{ x.Country }}{{ x.Country }}

  • 7/24/2019 AngularJS W3schhols

    50/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS lets you create dropdown lists based on items in an array, or anobject.

    If you want to create a dropdown list, based on a object or an array in

    AngularJS, you should use the ng-optiondirective:

    Examplevarapp = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.names = ["Emil", "Tobias", "Linus"];});

    You can also use the ng-repeatdirective to make the same dropdown list:

    Example{{x}}

    Because the ng-repeatdirective repeats a block of HTML code for each item inan array, it can be used to create options in a dropdown list, but the ng-

    optionsdirective was made especially for filling a dropdown list with options,

    and has at least one important advantage:

  • 7/24/2019 AngularJS W3schhols

    51/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Dropdowns made with ng-optionsallows the selected value to be an object,

    while dropdowns made from ng-repeathas to be a string.

    Assume you have an array of objects:

    $scope.cars = [

    {model : "Ford Mustang", color : "red"},

    {model : "Fiat 500", color : "white"},

    {model : "Volvo XC90", color : "black"}

    ];

    The ng-repeatdirective has it's limitations, the selected value must be a

    string:

    ExampleUsing ng-repeat:

    {{x.model}}

    You selected: {{selectedCar}}

    When using the ng-optionsdirective, the selected value can be an object:

    Example

    Using ng-options:

    You selected: {{selectedCar.model}}

    It's color is: {{selectedCar.color}}

    When the selected value can be an object, it can hold more information, and

    your application can be more flexible.

    We will use the ng-optionsdirective in this tutorial.

  • 7/24/2019 AngularJS W3schhols

    52/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    In the previous examples the data source was an array, but we can also use an

    object.

    Assume you have an object with key-value pairs:

    $scope.cars = {

    car01 : "Ford",

    car02 : "Fiat",

    car03 : "Volvo"

    };

    The expression in the ng-optionsattribute is a bit different for objects:

    ExampleUsing an object as the data source, xrepresents the key, and yrepresents the

    value:

    You selected: {{selectedCarl}}

    The selected value will always be the valuein a key-valuepair.

    The valuein a key-valuepair can also be an object:

    ExampleThe selected value will still be the valuein a key-valuepair, only this time it is

    an object:$scope.cars = {car01 : {brand : "Ford", model : "Mustang", color : "red"},car02 : {brand : "Fiat", model : "500", color : "white"},car03 : {brand : "Volvo", model : "XC90", color : "black"}

    };The options in the dropdown list does not have be the keyin a key-value pair,

    it can also be the value, or a property of the value object:

    Example

  • 7/24/2019 AngularJS W3schhols

    53/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The code from the previous chapter can also be used to read fromdatabases.

    AngularJS Example

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

    var app = angular.module('myApp', []);app.controller('customersCtrl', function($scope, $http) {

    $http.get("http://www.w3schools.com/angular/customers_mysql.php").then(function (response) {$scope.names = response.data.records;});

    });

  • 7/24/2019 AngularJS W3schhols

    54/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS Example

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

    var app = angular.module('myApp', []);app.controller('customersCtrl', function($scope, $http) {

    $http.get("http://www.w3schools.com/angular/customers_sql.aspx").then(function (response) {$scope.names = response.data.records;});

    });

    The following section is a listing of the server code used to fetch SQL data.

    1.

    Using PHP and MySQL. Returning JSON.

    2. Using PHP and MS Access. Returning JSON.

    3. Using ASP.NET, VB, and MS Access. Returning JSON.

    4.

    Using ASP.NET, Razor, and SQL Lite. Returning JSON.

  • 7/24/2019 AngularJS W3schhols

    55/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Requests for data from a different server (than the requesting page), are

    called cross-siteHTTP requests.

    Cross-site requests are common on the web. Many pages load CSS, images,

    and scripts from different servers.

    In modern browsers, cross-site HTTP requests from scriptsare restricted

    to same sitefor security reasons.

    The following line, in our PHP examples, has been added to allow cross-site

    access.

    header("Access-Control-Allow-Origin: *");

  • 7/24/2019 AngularJS W3schhols

    56/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

  • 7/24/2019 AngularJS W3schhols

    57/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    source=Northwind.mdb")

    objAdapter = New OledbDataAdapter("SELECT CompanyName, City, Country FROM

    Customers", conn)

    objAdapter.Fill(objDataSet, "myTable")

    objTable=objDataSet.Tables("myTable")

    outp = ""

    c = chr(34)

    for each x in objTable.Rows

    if outp "" then outp = outp & ","

    outp = outp & "{" & c & "Name" & c & ":" & c & x("CompanyName") & c & ","

    outp = outp & c & "City" & c & ":" & c & x("City") & c & ","

    outp = outp & c & "Country" & c & ":" & c & x("Country") & c & "}"

    next

    outp ="{" & c & "records" & c & ":[" & outp & "]}"response.write(outp)

    conn.close

    %>

    @{Response.AppendHeader("Access-Control-Allow-Origin", "*")

    Response.AppendHeader("Content-type", "application/json")

    var db = Database.Open("Northwind");

    var query = db.Query("SELECT CompanyName, City, Country FROM Customers");

    var outp =""

    var c = chr(34)

    }

    @foreach(var row in query)

    {

    if outp "" then outp = outp + ","

    outp = outp + "{" + c + "Name" + c + ":" + c + @row.CompanyName + c + ","

    outp = outp + c + "City" + c + ":" + c + @row.City + c + ","

    outp = outp + c + "Country" + c + ":" + c + @row.Country + c + "}"

    }

    outp ="{" + c + "records" + c + ":[" + outp + "]}"

    @outp

  • 7/24/2019 AngularJS W3schhols

    58/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS has directives for binding application data to the attributes ofHTML DOM elements.

    The ng-disableddirective binds AngularJS application data to the disabled

    attribute of HTML elements.

    AngularJS Example

    Click Me!

    Button

    {{ mySwitch }}

    Application explained:

    The ng-disableddirective binds the application data mySwitchto the HTML

    button's disabledattribute.

    The ng-modeldirective binds the value of the HTML checkbox element to the

    value ofmySwitch.

    If the value of mySwitchevaluates to true, the button will be disabled:

  • 7/24/2019 AngularJS W3schhols

    59/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Click Me!

    If the value of mySwitchevaluates to false, the button will not be disabled:

    Click Me!

    The ng-showdirective shows or hides an HTML element.

    AngularJS Example

    I am visible.

    I am not visible.

    The ng-show directive shows (or hides) an HTML element based on the valueof

    ng-show.

    You can use any expression that evaluates to true or false:

    AngularJS Example

    I am visible.

    In the next chapter, there are more examples, using the click of a

    button to hide HTML elements.

  • 7/24/2019 AngularJS W3schhols

    60/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-hidedirective hides or shows an HTML element.

    AngularJS Example

    I am not visible.

    I am visible.

  • 7/24/2019 AngularJS W3schhols

    61/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS has its own HTML events directives.

    You can add AngularJS event listeners to your HTML elements by using one or

    more of these directives:

    ng-blur ng-change

    ng-click

    ng-copy

    ng-cut

    ng-dblclick

    ng-focus

    ng-keydown

    ng-keypress

    ng-keyup ng-mousedown

    ng-mouseenter

    ng-mouseleave

    ng-mousemove

    ng-mouseover

    ng-mouseup

    ng-paste

    The event directives allows us to run AngularJS functions at certain user events.

    An AngularJS event will not overwrite an HTML event, both events will be

    executed.

  • 7/24/2019 AngularJS W3schhols

    62/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Mouse events occur when the cursor moves over an element, in this order:

    1.

    ng-mouseenter

    2.

    ng-mouseover

    3. ng-mousemove

    4.

    ng-mouseleave

    Or when a mouse button is clicked on an element, in this order:

    1.

    ng-mousedown

    2. ng-mouseup

    3.

    ng-click

    You can add mouse events on any HTML element.

    Example

    Increase the count variable when the mouse moves over the DIV element:

    Mouse over me!

    {{ count }}

    var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.count = 0;});

  • 7/24/2019 AngularJS W3schhols

    63/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-clickdirective defines AngularJS code that will be executed when the

    element is being clicked.

    Example

    Click me!

    {{ count }}

    var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.count = 0;});

    You can also refer to a function:

    Example

    Click me!

    {{ count }}

    var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.count = 0;$scope.myFunction = function() {

    $scope.count++;

    }});

  • 7/24/2019 AngularJS W3schhols

    64/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    If you want to show a section of HTML code when a button is clicked, and hide

    when the button is clicked again, like a dropdown menu, make the buttonbehave like a toggle switch:

    Click Me

    Example

    Click Me!

    Menu:PizzaPastaPesce

    var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.showMe = false;

    $scope.myFunc = function() {$scope.showMe = !$scope.showMe;

    }});

    The showMevariable starts out as the Boolean value false.

    ThemyFuncfunction sets the showMevariable to the opposite of what it is, by

    using the !(not) operator.

    You can pass the $eventobject as an argument when calling the function.

    The $eventobject contains the browser's event object:

  • 7/24/2019 AngularJS W3schhols

    65/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Example

    Mouse Over Me!

    Coordinates: {{x + ', ' + y}}

    var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.myFunc = function(myE) {$scope.x = myE.clientX;$scope.y = myE.clientY;

    }});

  • 7/24/2019 AngularJS W3schhols

    66/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Forms in AngularJS provides data-binding and validation of input controls.

    Input controls are the HTML input elements:

    input elements

    select elements

    button elements

    textarea elements

    Input controls provides data-binding by using the ng-modeldirective.

    The application does now have a property named firstname.

    The ng-modeldirective binds the input controller to the rest of your application.

    The property firstname, can be referred to in a controller:

    Examplevar app = angular.module('myApp', []);app.controller('formCtrl', function($scope) {

    $scope.firstname = "John";});

  • 7/24/2019 AngularJS W3schhols

    67/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    It can also be referred to elsewhere in the application:

    ExampleFirst Name:

    You entered: {{firstname}}

    A checkbox has the value trueor false. Apply the ng-modeldirective to acheckbox, and use it's value in your application.

    Example

    Show the header if the checkbox is checked:

    Check to show a header:

    My Header

    Bind radio buttons to your application with the ng-modeldirective.

    Radio buttons with the same ng-modelcan have different values, but only the

    selected one will be used.

    ExampleDisplay some text, based on the value of the selected radio button:

    Pick a topic:DogsTutorialsCars

  • 7/24/2019 AngularJS W3schhols

    68/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The value of myVar will be either dogs, tuts, or cars.

    Bind select boxes to your application with the ng-modeldirective.

    The property defined in the ng-modelattribute will have the value of the

    selected option in the selectbox.

    Example

    Display some text, based on the value of the selected option:

    Select a topic:

    DogsTutorialsCars

    The value of myVar will be either dogs, tuts, or cars.

    First Name:

    Last Name:

    RESET

    form = {"firstName":"John","lastName":"Doe"}

    master = {"firstName":"John","lastName":"Doe"}

  • 7/24/2019 AngularJS W3schhols

    69/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Application Code

    First Name:

    Last Name:


    RESET

    form = {{user}}

    master = {{master}}

    var app = angular.module('myApp', []);app.controller('formCtrl', function($scope) {

    $scope.master = {firstName: "John", lastName: "Doe"};$scope.reset = function() {$scope.user = angular.copy($scope.master);

    };$scope.reset();

    });

    The novalidateattribute is new in HTML5. It disables any default

    browser validation.

    The ng-appdirective defines the AngularJS application.

    The ng-controllerdirective defines the application controller.

    The ng-modeldirective binds two input elements to the userobject in the

    model.

    The formCtrlcontroller sets initial values to themasterobject, and defines

    the reset()method.

    The reset()method sets the userobject equal to the masterobject.

    The ng-clickdirective invokes the reset()method, only if the button is clicked.

    The novalidate attribute is not needed for this application, but normally you will

    use it in AngularJS forms, to override standard HTML5 validation.

  • 7/24/2019 AngularJS W3schhols

    70/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS can validate input data.

    AngularJS offers client-side form validation.

    AngularJS monitors the state of the form and input fields (input, textarea,

    select), and lets you notify the user about the current state.

    AngularJS also holds information about whether they have been touched, or

    modified, or not.

    You can use standard HTML5 attributes to validate input, or you can make your

    own validation functions.

    Client-side validation cannot alone secure user input. Server side

    validation is also necessary.

    Use the HTML5 attribute requiredto specify that the input field must be filled

    out:

    ExampleThe input field is required:

    The input's valid state is:

    {{myForm.myInput.$valid}}
  • 7/24/2019 AngularJS W3schhols

    71/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Use the HTML5 type emailto specify that the value must be an e-mail:

    Example

    The input field has to be an e-mail:

    The input's valid state is:

    {{myForm.myInput.$valid}}

    AngularJS is constantly updating the state of both the form and the input fields.

    Input fields have the following states:

    $untouchedThe field has not been touched yet

    $touchedThe field has been touched

    $pristineThe field has not been modified yet

    $dirtyThe field has been modified

    $invalidThe field content is not valid

    $validThe field content is valid

    They are all properties of the input field, and are either trueor false.

    Forms have the following states:

    $pristineNo fields have been modified yet

    $dirtyOne or more have been modified $invalidThe form content is not valid

    $validThe form content is valid

    $submittedThe form is submitted

    They are all properties of the form, and are either trueor false.

  • 7/24/2019 AngularJS W3schhols

    72/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    You can use these states to show meaningful messages to the user. Example, if

    a field is required, and the user leaves it blank, you should give the user a

    warning:

    ExampleShow an error message if the field has been touched AND is empty:The name isrequired.

    AngularJS adds CSS classes to forms and input fields depending on their states.

    The following classes are added to, or removed from, input fields:

    ng-untouchedThe field has not been touched yet

    ng-touchedThe field has been touched

    ng-pristineThe field has not been modified yet

    ng-dirtyThe field has been modified

    ng-validThe field content is valid

    ng-invalidThe field content is not valid

    ng-valid-keyOne keyfor each validation. Example: ng-valid-

    required, useful when there are more than one thing that must be

    validated

    ng-invalid-keyExample: ng-invalid-required

    The following classes are added to, or removed from, forms:

    ng-pristineNo fields has not been modified yet

    ng-dirtyOne or more fields has been modified

    ng-validThe form content is valid

    ng-invalidThe form content is not valid

    ng-valid-keyOne keyfor each validation. Example: ng-valid-

    required, useful when there are more than one thing that must be

    validated

    ng-invalid-keyExample: ng-invalid-required

  • 7/24/2019 AngularJS W3schhols

    73/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The classes are removed if the value they represent is false.

    Add styles for these classes to give your application a better and more intuitive

    user interface.

    Example

    Apply styles, using standard CSS:

    input.ng-invalid {

    background-color:pink;}input.ng-valid {

    background-color:lightgreen;}

    Forms can also be styled:

    Example

    Apply styles for unmodified (pristine) forms, and for modified forms:

    form.ng-pristine {

    background-color:lightblue;}form.ng-dirty {

    background-color:pink;}

    To create your own validation function is a bit more tricky. You have to add a

    new directive to your application, and deal with the validation inside a function

    with certain specified arguments.

  • 7/24/2019 AngularJS W3schhols

    74/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ExampleCreate your own directive, containing a custom validation function, and refer to

    it by usingmy-directive.

    The field will only be valid if the value contains the character "e":

    varapp = angular.module('myApp', []);app.directive('myDirective', function() {

    return{require: 'ngModel',link: function(scope, element, attr, mCtrl) {functionmyValidation(value) {if(value.indexOf("e") > -1) {

    mCtrl.$setValidity('charE', true);} else{mCtrl.$setValidity('charE', false);

    }returnvalue;

    }mCtrl.$parsers.push(myValidation);

    }};

    });

    In HTML, the new directive will be referred to by using the attributemy-

    directive.

    In the JavaScript we start by adding a new directive namedmyDirective.

    Remember, when naming a directive, you must use a camel case

    name,myDirective, but when invoking it, you must use -separatedname,my-directive.

    Then, return an object where you specify that we require ngModel, which is the

    ngModelController.

  • 7/24/2019 AngularJS W3schhols

    75/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Make a linking function which takes some arguments, where the fourth

    argument,mCtrl, is the ngModelController,

    Then specify a function, in this case namedmyValidation, which takes one

    argument, this argument is the value of the input element.

    Test if the value contains the letter "e", and set the validity of the model

    controller to either trueor false.

    At last,mCtrl.$parsers.push(myValidation);will addthemyValidationfunction to an array of other functions, which will be

    executed every time the input value changes.

    Validation Example

    Validation Example

    Username:
    Username is required.

    Email:

    Email is required.Invalid email address.

  • 7/24/2019 AngularJS W3schhols

    76/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    var app = angular.module('myApp', []);

    app.controller('validateCtrl', function($scope) {$scope.user = 'John Doe';$scope.email = '[email protected]';

    });

    The HTML form attribute novalidateis used to disable defaultbrowser validation.

    The AngularJS directive ng-modelbinds the input elements to the model.

    The model object has two properties: userand email.

    Because of ng-show, the spans with color:red are displayed only when user or

    email is $dirtyand $invalid.

  • 7/24/2019 AngularJS W3schhols

    77/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    API stands for Application Programming Interface.

    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

  • 7/24/2019 AngularJS W3schhols

    78/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Example

    {{ x1 }}

    {{ x2 }}

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

    Example

    {{ x1 }}

    {{ x2 }}

    var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.x1 = "John";$scope.x2 = angular.uppercase($scope.x1);});

    Example

    {{ x1 }}

    {{ x2 }}

    var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {$scope.x1 = "JOHN";$scope.x2 = angular.isString($scope.x1);});
  • 7/24/2019 AngularJS W3schhols

    79/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Example

    {{ x1 }}

    {{ x2 }}

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

  • 7/24/2019 AngularJS W3schhols

    80/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    You can easily use w3.css style sheet together with AngularJS. This chapterdemonstrates how.

    To include W3.CSS in your AngularJS application, add the following line to the

    head of your document:

    If you want to study W3.CSS, visit W3.CSS Tutorial.

    Below is a complete HTML example, with all AngularJS directives and W3.CSS

    classes explained.

    HTML Code

    Users

    Edit

    First Name

    Last Name

    http://www.w3schools.com/w3css/default.asphttp://www.w3schools.com/w3css/default.asphttp://www.w3schools.com/w3css/default.asp
  • 7/24/2019 AngularJS W3schhols

    81/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Edit

    {{ user.fName }}{{ user.lName }}


    Create New User

    Create New User:

    Edit User:First Name:


    Last Name:


    Password:


    Repeat:


    Save Changes

  • 7/24/2019 AngularJS W3schhols

    82/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS

    Directive

    Description

  • 7/24/2019 AngularJS W3schhols

    83/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Element Class Defines

    w3-container A content container

    w3-table A table

    w3-bordered A bordered table

    w3-striped A striped table

    w3-btn A button

    w3-green A green button

    w3-ripple A ripple effect when you click the button

    w3-input An input field

    w3-border A border on the input field

  • 7/24/2019 AngularJS W3schhols

    84/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    myUsers.js

    angular.module('myApp', []).controller('userCtrl', function($scope) {$scope.fName = '';

    $scope.lName = '';

    $scope.passw1 = '';

    $scope.passw2 = '';

    $scope.users = [

    {id:1, fName:'Hege', lName:"Pege"},

    {id:2, fName:'Kim', lName:"Pim"},

    {id:3, fName:'Sal', lName:"Smith"},

    {id:4, fName:'Jack', lName:"Jones"},

    {id:5, fName:'John', lName:"Doe"},{id:6, fName:'Peter',lName:"Pan"}

    ];

    $scope.edit = true;

    $scope.error = false;

    $scope.incomplete = false;

    $scope.hideform = true;

    $scope.editUser = function(id) {

    $scope.hideform = false;

    if(id == 'new') {

    $scope.edit = true;

    $scope.incomplete = true;

    $scope.fName = '';

    $scope.lName = '';

    } else{

    $scope.edit = false;

    $scope.fName = $scope.users[id-1].fName;

    $scope.lName = $scope.users[id-1].lName;

    }

    };

    $scope.$watch('passw1',function() {$scope.test();});$scope.$watch('passw2',function() {$scope.test();});

    $scope.$watch('fName', function() {$scope.test();});

    $scope.$watch('lName', function() {$scope.test();});

    $scope.test = function() {

    if($scope.passw1 !== $scope.passw2) {

  • 7/24/2019 AngularJS W3schhols

    85/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    $scope.error = true;

    } else{

    $scope.error = false;

    }

    $scope.incomplete = false;

    if($scope.edit && (!$scope.fName.length ||!$scope.lName.length ||

    !$scope.passw1.length || !$scope.passw2.length)) {

    $scope.incomplete = true;

    }

    };

    });

    Scope Properties Used for

    $scope.fName Model variable (user first name)

    $scope.lName Model variable (user last name)

    $scope.passw1 Model variable (user password 1)

    $scope.passw2 Model variable (user password 2)

    $scope.users Model variable (array of users)

    $scope.edit Set to true when user clicks on 'Create user'.

    $scope.hideform Set to true when user clicks on 'Edit' or 'Create user'.

    $scope.error Set to true if passw1 not equal passw2

  • 7/24/2019 AngularJS W3schhols

    86/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    $scope.incomplete Set to true if any field is empty (length = 0)

    $scope.editUser Sets model variables

    $scope.$watch Watches model variables

    $scope.test Tests model variables for errors and incompleteness

  • 7/24/2019 AngularJS W3schhols

    87/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    With AngularJS, you can include HTML from an external file.

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

    Example

    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:

  • 7/24/2019 AngularJS W3schhols

    88/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Example

    varapp = angular.module('myApp', []);app.controller('customersCtrl', function($scope, $http) {

    $http.get("customers.php").then(function(response) {$scope.names = response.data.records;

    });});

    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:varapp = angular.module('myApp', [])app.config(function($sceDelegateProvider) {

    $sceDelegateProvider.resourceUrlWhitelist(['http://www.refsnesdata.no/**'

    ]);});

    Be sure that the server on the destination allows cross domain file

    access.

  • 7/24/2019 AngularJS W3schhols

    89/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    AngularJS provides animated transitions, with help from CSS.

    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.

    To make your applications ready for animations, you must include the AngularJS

    Animate library:

    Then you must refer to the ngAnimatemodule in your application:

  • 7/24/2019 AngularJS W3schhols

    90/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Or if your application has a name, add ngAnimateas a dependency in your

    application module:

    ExampleHide the DIV: varapp = angular.module('myApp', ['ngAnimate']);

    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-showand ng-hidedirectives adds or removes a ng-hideclass value.

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

    The ng-repeatdirective also adds a ng-moveclass value when the HTML

    element changes position.

  • 7/24/2019 AngularJS W3schhols

    91/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    In addition, duringthe animation, the HTML element will have a set of class

    values, which will be removed when the animation has finished. Example:

    the ng-hidedirective 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)

    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 CSS Transition Tutorialand CSS

    Animation Tutorial.

    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-hideclass, 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;}

    http://www.w3schools.com/css/css3_transitions.asphttp://www.w3schools.com/css/css3_transitions.asphttp://www.w3schools.com/css/css3_animations.asphttp://www.w3schools.com/css/css3_animations.asphttp://www.w3schools.com/css/css3_animations.asphttp://www.w3schools.com/css/css3_animations.asphttp://www.w3schools.com/css/css3_animations.asphttp://www.w3schools.com/css/css3_transitions.asp
  • 7/24/2019 AngularJS W3schhols

    92/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    CSS Animations allows you to change CSS property values smoothly, from one

    value to another, over a given duration:

    ExampleWhen the DIV element gets the .ng-hideclass, themyChangeanimation 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;}

  • 7/24/2019 AngularJS W3schhols

    93/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    It is time to create a real AngularJS Single Page Application (SPA).

    You have learned more than enough to create your first AngularJS application:

    My Note

    Save Clear

    Number of characters left: 100

    AngularJS Example

    My Note

  • 7/24/2019 AngularJS W3schhols

    94/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    SaveClear

    Number of characters left:

    The application file "myNoteApp.js":

    var app = angular.module("myNoteApp", []);

    The controller file "myNoteCtrl.js":

    app.controller("myNoteCtrl", function($scope) {

    $scope.message = "";

    $scope.left = function() {return 100 - $scope.message.length;};

    $scope.clear = function() {$scope.message = "";};

    $scope.save = function() {alert("Note Saved");};

    });

    The element is the container of the AngularJS application: ng-

    app="myNoteApp":

    A a in the HTML page is the scope of a controller: ng-

    controller="myNoteCtrl":

    An ng-modeldirective binds a to the controller variable message:

  • 7/24/2019 AngularJS W3schhols

    95/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The two ng-clickevents invoke the controller functions clear()and save():

    Save

    Clear

    An ng-binddirective binds the controller function left()to a displaying

    the characters left:

    Number of characters left:

    Your application libraries are added to the page (after the library):

    Above you have the skeleton (scaffolding) of a real life AngularJS, single page

    application (SPA).

    The element is the "container" for the AngularJS application (ng-

    app=).

    A elements defines the scope of an AngularJS controller (ng-controller=).

    You can have many controllers in one application.

    An application file (my...App.js) defines the application module code.

    One or more controller files (my...Ctrl.js) defines the controller code.

  • 7/24/2019 AngularJS W3schhols

    96/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-app directive is placed at the root element of the application.

    For single page applications (SPA), the root of the application is often the

    element.

    One or more ng-controller directives define the application controllers. Each

    controller has its own scope: the HTML element where they were defined.

    AngularJS starts automatically on the HTML DOMContentLoaded event. If an ng-

    app directive is found, AngularJS will load any module named in the directive,

    and compile the DOM with ng-app as the root of the application.

    The root of the application can be the whole page, or a smaller portion of the

    page. The smaller the portion, the faster the application will compile and

    execute.

  • 7/24/2019 AngularJS W3schhols

    97/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-appDefines the root element of an application.

    ExampleLet the body element become the root element for the AngularJS application:

    My first expression: {{ 5 + 5 }}

    The ng-appdirective tells AngularJS that this is the root element of theAngularJS application.

    All AngularJS applications must have a root element.

    You can only have one ng-appdirective in your HTML document. If more than

    one ng-appdirective appears, the first appearance will be used.

    ...

    content inside the ng-app root element can contain AngularJS code

    ...

    Supported by all HTML elements.

  • 7/24/2019 AngularJS W3schhols

    98/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Value Description

    modulename Optional. Specifies the name of a module to load with the application

    ExampleLoad a module to run in the application

    {{ firstName + " "+ lastName }}

    varapp = angular.module("myApp", []);app.controller("myCtrl", function($scope) {

    $scope.firstName = "John";$scope.lastName = "Doe";

    });

    ng-bind

    ExampleBind the innerHTML of the

    element to the variable myText:

    The ng-binddirective tells AngularJS to replace the content of an HTML

    element with the value of a given variable, or expression.

    If the value of the given variable, or expression, changes, the content of the

    specified HTML element will be changed as well.

  • 7/24/2019 AngularJS W3schhols

    99/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Or as a CSS class:

    Supported by all HTML elements.

    Value Description

    expression Specifies a variable, or an expression to evaluate.

    ng-bind-html

    ExampleBind the innerHTML of the

    element to the variable myText:

    var app = angular.module("myApp", ['ngSanitize']);

    app.controller("myCtrl", function($scope) {$scope.myText = "My name is: John Doe";

    });

  • 7/24/2019 AngularJS W3schhols

    100/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-bind-htmldirective is a secure way of binding content to an HTML

    element.

    When you are letting AngularJS write HTML in your application, you should

    check the HTML for dangerous code. By including the "angular-santize.js"

    module in your application you can do so by running the HTML code through the

    ngSanitize function.

    Supported by all HTML elements.

    Value Description

    expression Specifies a variable, or an expression to evaluate.

  • 7/24/2019 AngularJS W3schhols

    101/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-bind-template

    ExampleBind two expressions to the

    element:

    var app = angular.module("myApp", []);app.controller("myCtrl", function($scope) {

    $scope.firstName = "John";$scope.lastName = "Doe";

    });

    The ng-bind-templatedirective tells AngularJS to replace the content of an

    HTML element with the value of the given expressions.

    Use the ng-bind-templatedirective when you want to bind more thant one

    expression to your HTML element.

    Supported by all HTML elements.

    Value Description

    expression One or more expressions to evaluate, each surrounded by {{ }}.

  • 7/24/2019 AngularJS W3schhols

    102/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-blur

    ExampleExecute an expresson when the input field loses focus (onblur):

    {{count}}

    The ng-blurdirective tells AngularJS what to do when an HTML element loses

    focus.

    The ng-blurdirective from AngularJS will not override the element's original

    onblur event, both the ng-blurexpression and the original onblur event will be

    executed.

    Supported by , , , , and the window object.

    Value Description

    expression An expression to execute when an element loses focus.

  • 7/24/2019 AngularJS W3schhols

    103/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-change

    ExampleExecute a function when the value of the input field changes:

    The input field has changed {{count}}times.

    angular.module('myApp', []).controller('myCtrl', ['$scope', function($scope) {

    $scope.count = 0;$scope.myFunc = function() {

    $scope.count++;

    };}]);

    The ng-changedirective tells AngularJS what to do when the value of an HTML

    element changes.

    The ng-changedirective requires a ng-modeldirective to be present.

    The ng-changedirective from AngularJS will not override the element's original

    onchange event, both the ng-changeexpression and the original onchange

    event will be executed.

    The ng-changeevent is triggered at every change in the value. It will not wait

    until all changes are made, or when the input field loses focus.

    The ng-changeevent is only triggered if there is a actual change in the input

    value, and not if the change was made from a JavaScript.

  • 7/24/2019 AngularJS W3schhols

    104/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Supported by , , and .

    Value Description

    expression An expression to execute when an element's value changes.

    ng-checked

    ExampleCheck one to check them all:

    My cars:

    Check all

    Volvo
    Ford
    Mercedes

    The ng-checkeddirective sets the checked attribute of a checkbox or a

    radiobutton.

    The checkbox, or radiobutton, will be checked if the expression inside the ng-

    checkedattribute returns true.

  • 7/24/2019 AngularJS W3schhols

    105/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    Supported by elements of type checkbox or radio.

    Value Description

    expression An expression that will set the element's checked attribute if it returns true.

    ng-class

    ExampleChange class of a element:

    Sky

    Tomato

    Welcome Home!

    I like it!

    The ng-classdirective dynamically binds one or more CSS classes to an HTML

    element.

    The value of the ng-classdirective can be a string, an object, or an array.

    If it is a string, it should contain one or more, space-separated class names.

  • 7/24/2019 AngularJS W3schhols

    106/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    As an object, it should contain key-value pairs, where the key is a boolean

    value, and the value is the class name of the class you want to add. The class

    will only be added if the key is set to true.

    As an array, it can be a combination of both. Each array element can be either astring, or an object, described as above.

    Supported by all HTML elements.

    Value Description

    expression An expression that returns one or more class names.

    ng-class-evenExample

    Set class="striped" for every other (even) table row:

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

  • 7/24/2019 AngularJS W3schhols

    107/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    The ng-class-evendirective dynamically binds one or more CSS classes to an

    HTML element, but will take effect only on every other (even) appearence of theHTML element.

    The ng-class-evendirective will only work if it is used together with the ng-

    repeatdirective.

    The ng-class-evendirective is perfect for styling items in a list or rows in a

    table, but it can be used on any HTML element.

    Supported by all HTML elements.

    Value Description

    expression An expression that returns one or more class names.

  • 7/24/2019 AngularJS W3schhols

    108/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-class-odd

    ExampleSet class="striped" for every other (odd) table row:

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

    The ng-class-odddirective dynamically binds one or more CSS classes to an

    HTML element, but will take effect only on every other (odd) appearence of the

    HTML element.

    The ng-class-odddirective will only work if it is used together with the ng-

    repeatdirective.

    The ng-class-odddirective is perfect for styling items in a list or rows in a

    table, but it can be used on any HTML element.

    Supported by all HTML elements.

    Value Description

    expression An expression that returns one or more class names.

  • 7/24/2019 AngularJS W3schhols

    109/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-click

    ExampleIncrease the count variable by one, each time the button is clicked:

    OK

    The ng-clickdirective tells AngularJS what to do when an HTML element is

    clicked.

    Supported by all HTML elements.

    Value Description

    expression An expression to execute when an element is clicked.

    ExampleExecute a function, in AngularJS, when a button is clicked:

    OK

    The button has been clicked {{count}}times.

    angular.module('myApp', []).controller('myCtrl', ['$scope', function($scope) {

    $scope.count = 0;$scope.myFunc = function() {

    $scope.count++;};

    }]);

  • 7/24/2019 AngularJS W3schhols

    110/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-cloak

    ExamplePrevent the application from flicker at page load:

    {{ 5 + 5 }}

    The ng-cloakdirective prevents the document from showing unfinished

    AngularJS code while AngularJS is being loaded.

    AngularJS applications can make HTML documents flicker when the application

    is being loaded, showing the AngularJS code for a second, before all code are

    executed. Use the ng-cloakdirective to prevent this.

    Supported by all HTML elements.

    The ng-cloakdirective has no parameters.

  • 7/24/2019 AngularJS W3schhols

    111/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-controller

    ExampleAdd a controller to handle your application variables:

    Full Name: {{firstName + " " + lastName}}var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) {

    $scope.firstName = "John";$scope.lastName = "Doe";

    });

    The ng-controllerdirective adds a controller to your application.

    In the controller you can write code, and make functions and variables, which

    will be parts of an object, available inside the current HTML element. In

    AngularJS this object is called a scope.

    Supported by all HTML elements.

    Value Description

    expression The name of the controller.

  • 7/24/2019 AngularJS W3schhols

    112/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-copy

    ExampleExecute an expresson when the text of the input field is being copied:

    The ng-copydirective tells AngularJS what to do when an HTML element is

    being copied.

    The ng-copydirective from AngularJS will not override the element's original

    oncopy event, both the ng-copyexpression and the original oncopy event will

    be executed.

    Supported by all HTML elements.

    Value Description

    expression An expression to execute when the text of an element is being copied.

  • 7/24/2019 AngularJS W3schhols

    113/180

    Created By www.ebooktutorials.in

    Content Downloaded from www.w3schools.com

    ng-csp

    ExampleChange the way AngularJS behaves regarding "eval" and inline styles:

    ...

    The ng-cspdirective is used to change the security policy of AngularJS.

    With the ng-cspdirective set, AngularJS will not run any eval functions, and it

    will not inject any inline styles.

    Setting the value of the ng-cspdirective to no-unsafe-eval, will stop