How to use AngularJs Expressions

Points To Remember

You can evaluate angular expressions by the following ways

  • using interpolation {{ }}
  • using ng-bind

Examples of AngularJs Expressions

Following are the ways you can use the angular expressions.

  • Mathematical calculations 
  • String operations
  • Json operation
  • Array operations
You may want to see ng-init.

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  </head>

  <body ng-app="" ng-init="firstName='Ekansh';lastName='Rastogi';json=[{'name':'user1','age':'18'},{'name':'user2','age':'24'}];array=[0,1,2,3,8]">
    <div ng-bind="firstName + ' ' + lastName"></div><hr/>
    
    String operation ->{{"Hello" +" " + "World!" }}<hr/>
    
    Math operation -> {{2 * 5 }}<hr/>
    
    String &amp; Math operation ->{{"10 * 15 * 84  = "  + (10 * 15 * 84)}}<hr/>
    
    Json Operation ->{{'name = ' +json[0].name + ' age =' + json[0].age}}<br/>
    
    {{'name = ' +json[1].name + ' age =' + json[1].age}}<hr/>
    
    Array operation -> <span ng-bind="{{array}}"></span>
  </body>

</html>

The above code will give you the following output.

No comments :

Post a Comment