AngularJS


Built-in directives All Versions

0.9.0
0.9.1
0.9.2
0.9.3
0.9.4
0.9.5
0.9.6
0.9.7
0.9.9
0.9.10
0.9.11
0.9.12
0.9.13
0.9.14
0.9.15
0.9.16
0.9.17
0.9.18
0.9.19
0.10.0
0.10.1
0.10.2
0.10.3
0.10.4
0.10.5
0.10.6
1.0.0rc1
g3-v1.0.0rc1
g3-v1.0.0-rc2
v1.0.0rc2
v1.0.0rc3
v1.0.0rc4
v1.0.0rc5
v1.0.0rc6
v1.0.0rc7
v1.0.0rc8
v1.0.0rc9
v1.0.0rc10
v1.0.0rc11
v1.0.0rc12
1.0.0
1.0.1
1.0.2
1.1.0
1.0.3
1.1.1
1.0.4
1.1.2
1.0.5
1.1.3
1.0.6
1.1.4
1.0.7
1.1.5
1.2.0rc1
1.0.8
1.2.0-rc.2
1.2.0-rc.3
1.2.0
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.6
1.2.7
1.2.8
1.2.9
1.2.10
1.2.11
1.2.12
1.2.13
1.2.14
1.3.0-beta.1
1.3.0-beta.2
1.3.0-beta.3
1.2.15
1.3.0-beta.4
1.2.16
1.3.0-beta.5
1.3.0-beta.6
1.3.0-beta.7
1.3.0-beta.8
1.3.0-beta.9
1.3.0-beta.10
1.2.17
1.3.0-beta.11
1.2.18
1.3.0-beta.12
1.3.0-beta.13
1.2.19
1.3.0-beta.14
1.2.20
1.3.0-beta.15
1.3.0-beta.16
1.2.21
1.3.0-beta.17
1.2.22
1.3.0-beta.18
1.2.23
1.3.0-beta.19
1.3.0-rc.0
1.2.24
1.3.0-rc.1
1.2.25
1.3.0-rc.2
1.3.0-rc.3
1.3.0-rc.4
1.2.26
1.3.0-rc.5
1.3.0
1.3.1
1.3.2
1.3.3
1.2.27
1.3.4
1.3.5
1.3.6
1.3.7
1.2.28
1.3.8
1.4.0-beta.0
1.3.9
1.3.10
1.4.0-beta.1
1.3.11
1.4.0-beta.2
1.3.12
1.4.0-beta.3
1.3.13
1.4.0-beta.4
1.3.14
1.4.0-beta.5
1.3.15
1.4.0-beta.6
1.4.0-rc.0
1.4.0-rc.1
1.4.0-rc.2
1.4.0
1.3.16
1.4.1
1.3.17
1.4.2
1.4.3
1.4.4
1.3.18
1.4.5
1.3.19
1.4.6
1.5.0-beta.0
1.2.29
1.3.20
1.4.7
1.5.0-beta.1
1.5.0-beta.2
1.4.8
1.5.0-rc.0
1.5.0-rc.1
1.4.9
1.5.0-rc.2
1.5.0
1.4.10
1.5.1
1.5.2
1.5.3
1.5.4
1.5.5
1.4.11
1.5.6
1.4.12
1.5.7
1.2.30
1.5.8

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 24

    This example demonstrates how Angular expressions are evaluated when using type="text" and type="number" for the input element. Consider the following controller and view:

    Controller

     var app = angular.module('app', []);
    
     app.controller('ctrl', function($scope) {
         $scope.textInput = {
             value: '5'
         };     
         $scope.numberInput = {
             value: 5
         };
     });
    

    View

    <div ng-app="app" ng-controller="ctrl">
        <input type="text" ng-model="textInput.value">
        {{ textInput.value + 5 }}
        <input type="number" ng-model="numberInput.value">
        {{ numberInput.value + 5 }}
    </div>
    
    • When using + in an expression bound to text input, the operator will concatenate the strings (first example), displaying 55 on the screen*.
    • When using + in an expression bound to number input, the operator return the sum of the numbers (second example), displaying 10 on the screen*.

    * - That is until the user changes the value in the input field, afterward the display will change accordingly.

    Working Example

  • 7

    The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. - View source

    HTML

    <div ng-cloak>
      <h1>Hello {{ name }}</h1>
    </div>
    

    ngCloak can be applied to the body element, but the preferred usage is to apply multiple ngCloak directives to small portions of the page to permit progressive rendering of the browser view.

    The ngCloak directive has no parameters.

    See also: Preventing flickering

  • 6

    ng-if is a directive similar to ng-show but inserts or removes the element from the DOM instead of simply hiding it. Angular 1.1.5 introduced ng-If directive. You can Use ng-if directive above 1.1.5 versions. This is useful because Angular will not process digests for elements inside a removed ng-if reducing the workload of Angular especially for complex data bindings.

    Unlike ng-show, the ng-if directive creates a child scope which uses prototypal inheritance. This means that setting a primitive value on the child scope will not apply to the parent. To set a primitive on the parent scope the $parent property on the child scope will have to be used.

    JavaScript

    angular.module('MyApp', []);
    
    angular.module('MyApp').controller('myController', ['$scope', '$window', function myController($scope, $window) {
        $scope.currentUser= $window.localStorage.getItem('userName');
    }]);
    

    View

    <div ng-controller="myController">
        <div ng-if="currentUser">
            Hello, {{currentUser}}
        </div>
        <div ng-if="!currentUser">
            <a href="/login">Log In</a>
            <a href="/register">Register</a>
        </div>
    </div>
    

    DOM If currentUser Is Not Undefined

    <div ng-controller="myController">
        <div ng-if="currentUser">
            Hello, {{currentUser}}
        </div>
        <!-- ng-if: !currentUser -->
    </div>
    

    DOM If currentUser Is Undefined

    <div ng-controller="myController">
        <!-- ng-if: currentUser -->
        <div ng-if="!currentUser">
            <a href="/login">Log In</a>
            <a href="/register">Register</a>
        </div>
    </div>
    

    Working Example

    Function Promise

    The ngIf directive accepts functions as well, which logically require to return true or false.

    <div ng-if="myFunction()">
        <span>Span text</span>
    </div>
    

    The span text will only appear if the function returns true.

    $scope.myFunction = function() {
        var result = false;
        // Code to determine the boolean value of result 
        return result;
    };
    

    As any Angular expression the function accepts any kind of variables.

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Built-in directives? Ask Question

Topic Outline