Step 1 – AngularJs Expressions

Definition– AngularJS set data to html.

Syntax – There are 2 ways-

i- {{ }}

ii- ng-bind = “”

Now look on below exmaple, you can copy this html code, and save as anyName.html and double click to open it in browser,


AngularJS Expressions Example

<div ng-controller="MyController">
    <!-- Displaying a variable value using curly braces -->
    <p>{{myVariable}}</p>

    <!-- Displaying a variable value using ng-bind -->
    <p ng-bind="myVariable"></p>

    <!-- Defining the variable in the controller -->
    <script>
        var app = angular.module('myApp', []);
        app.controller('MyController', function($scope) {
            $scope.myVariable = 'Hello World';
            $scope.myFunction = function() {
                return 'Hello World';
            };
            $scope.showElement = true;
            $scope.itemList = ['Item 1', 'Item 2', 'Item 3'];
        });
    </script>

    <!-- Calling a function using curly braces -->
    <p>{{myFunction()}}</p>

    <!-- Calling a function using ng-bind -->
    <p ng-bind="myFunction()"></p>

    <!-- Conditional rendering -->
    <div ng-if="showElement">
        <p>This element will be displayed if showElement is true.</p>
    </div>

    <!-- Iterating over a list using curly braces -->
    <ul>
        <li ng-repeat="item in itemList">{{item}}</li>
    </ul>

    <!-- Iterating over a list using ng-bind -->
    <ul>
        <li ng-repeat="item in itemList" ng-bind="item"></li>
    </ul>
</div>
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.