Category: 01. Angular Js

https://cdn3d.iconscout.com/3d/free/thumb/free-angular-3d-logo-download-in-png-blend-fbx-gltf-file-formats–technology-social-media-pack-logos-4642763.png

  • AngularJS HTML DOM

    In AngularJS, some directives can be used to bind application data to attributes of HTML DOM elements.These directives are:

    DirectiveDescription
    ng-disabledIt disables a given control.
    ng-showIt shows a given control.
    ng-hideIt hides a given control.
    ng-clickIt represents an AangularJS click event.

    ng-disabled directive:The ng-disabled directive binds AngularJS application data to the disabled attribute of HTML elements. In the below code, it binds a model to a checkbox.

    <input type = "checkbox" ng-model = "enableDisableButton">Disable Button  
    
    button ng-disabled = "enableDisableButton">Click Me!</button> 

      ng-show directive: The ng-show directive shows or hides an HTML element. In the below code, it binds a model to a checkbox.

      <input type = "checkbox" ng-model = "showHide1">Show Button  
      
      button ng-show = "showHide1">Click Me!</button>

      ng-hide directive: The ng-hide directive hides or shows an HTML element. In the below code, it binds a model to a checkbox.

      <input type = "checkbox" ng-model = "showHide2">Hide Button  
      
      <button ng-hide = "showHide2">Click Me!</button>  

        ng-click directive: The ng-click directive counts the total clicks an HTML element. In the below code, it binds a model to a checkbox.

        <p>Total click: {{ clickCounter }}</p>  
        
        lt;button ng-click = "clickCounter = clickCounter + 1">Click Me!</button>  

          Let’s take an example to deploy the all above directives and test the variations:

          See this example:

          <!DOCTYPE html>  
          
          <html>  
          
          <head>  
          
                <title>AngularJS HTML DOM</title>  
          
          </head>  
          
          <body>  
          
                <h2>AngularJS Sample Application</h2>  
          
                <div ng-app = "">  
          
                     <table border = "0">  
          
                      <tr>  
          
                         <td><input type = "checkbox" ng-model = "enableDisableButton">Disable Button</td>  
          
                         <td><button ng-disabled = "enableDisableButton">Click Me!</button></td>  
          
                      </tr>  
          
                      <tr>  
          
                         <td><input type = "checkbox" ng-model = "showHide1">Show Button</td>  
          
                         <td><button ng-show = "showHide1">Click Me!</button></td>  
          
                      </tr>  
          
                       <tr>  
          
                         <td><input type = "checkbox" ng-model = "showHide2">Hide Button</td>  
          
                         <td><button ng-hide = "showHide2">Click Me!</button></td>  
          
                      </tr>  
          
                       <tr>  
          
                         <td><p>Total click: {{ clickCounter }}</p></td>  
          
                         <td><button ng-click = "clickCounter = clickCounter + 1">Click Me!</button></td>  
          
                      </tr>  
          
                   </table>  
          
                   </div>  
          
          <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>  
          
          </body>  
          
          </html>
        1. AngularJS Select

          In AngularJS, you can create a dropdown list (select box) based on items in an array, or an object.

          Using ng-options

          You should use the ng-option directive to create a dropdown list, based on an object or an array in AngularJS.

          See this example:

          <!DOCTYPE html>  
          
          <html>  
          
          <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
          
          <body>  
          
          <div ng-app="myApp" ng-controller="myCtrl">  
          
          <select ng-model="selectedName" ng-options="x for x in names">  
          
          </select>  
          
          </div>  
          
          <script>  
          
          var app = angular.module('myApp', []);  
          
          app.controller('myCtrl', function($scope) {  
          
              $scope.names = ["Java", "PHP", ".Net", "AngularJS", "C/C++"];  
          
          });  
          
          </script>  
          
          </body>  
          
          </html>

          Note:

          You can also use the ng-repeat directive to make the same dropdown list as ng-options.

          See this example:

          <!DOCTYPE html>  
          
          <html>  
          
          <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
          
          <body>  
          
          <div ng-app="myApp" ng-controller="myCtrl">  
          
          <select>  
          
          <option ng-repeat="x in names">{{x}}</option>  
          
          </select>  
          
          </div>  
          
          <script>  
          
          var app = angular.module('myApp', []);  
          
          app.controller('myCtrl', function($scope) {  
          
                  $scope.names = ["Java", "PHP", ".Net", "AngularJS", "C/C++"];  
          
          });  
          
          </script>  
          
          <p>The same example of dropdown list using the ng-repeat directive.</p>  
          
          </body>  
          
          </html>

          ng-options vs. ng-repeat

          Although, both can be used for dropdown list, but ng-repeat directive repeats a block of HTML code for each item in an array, it can be used to create options in a dropdown list, while the ng-options directive was made especially for filling a dropdown list with options, and has at least one important advantage:

          Dropdowns made with ng-options allows the selected value to be an object, while dropdowns made from ng-repeat has to be a string.

          Consider that you have an array of objects:

          $scope.cars = [  
          
              {model : "Ford Mustang", color : "red"},  
          
              {model : "Fiat 500", color : "white"},  
          
              {model : "Volvo XC90", color : "black"}  
          
          ]; 

            Limitation of ng-repeat

            The ng-repeat directive has a limitation that the selected value must be a string:

            See this example:

            <!DOCTYPE html>  
            
            <html>  
            
            <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
            
            <body>  
            
            <div ng-app="myApp" ng-controller="myCtrl">  
            
            <p>Select a car:</p>  
            
            <select ng-model="selectedCar">  
            
            <option ng-repeat="x in cars" value="{{x.model}}">{{x.model}}</option>  
            
            </select>  
            
            <h1>You selected: {{selectedCar}}</h1>  
            
            </div>  
            
            <script>  
            
            var app = angular.module('myApp', []);  
            
            app.controller('myCtrl', function($scope) {  
            
                $scope.cars = [  
            
                    {model : "Ford", color : "red"},  
            
                    {model : "Honda", color : "white"},  
            
                    {model : "Volvo", color : "black"},  
            
                    {model : "Hundai", color : "gray"}  
            
                ];  
            
            });  
            
            </script>  
            
            </body>  
            
            </html>

            While using the ng-options directive, you can select an object value:

            See this example:

            <!DOCTYPE html>  
            
            <html>  
            
            <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
            
            <body>  
            
            <div ng-app="myApp" ng-controller="myCtrl">  
            
            <p>Select a car:</p>  
            
            <select ng-model="selectedCar" ng-options="x.model for x in cars">  
            
            </select>  
            
            <h1>You selected: {{selectedCar.model}}</h1>  
            
            <p>It's color is: {{selectedCar.color}}</p>  
            
            </div>  
            
            <script>  
            
            var app = angular.module('myApp', []);  
            
            app.controller('myCtrl', function($scope) {  
            
                   $scope.cars = [  
            
                    {model : "Ford", color : "red"},  
            
                    {model : "Honda", color : "white"},  
            
                    {model : "Volvo", color : "black"},  
            
                    {model : "Hundai", color : "gray"}  
            
                ];  
            
              
            
            });  
            
            </script>  
            
            </body>  
            
            </html>

            Use data source as an object

            We can also use data source as an object.

            Consider that you have an object with following key-value pairs:

            $scope.cars = {  
            
                    car01 : "Ford",  
            
                    car02 : "Honda",  
            
                    car03 : "Volvo",  
            
                    car03 : "Hundai",  
            
                }; 

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

              See this example:

              <!DOCTYPE html>  
              
              <html>  
              
              <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
              
              <body>  
              
              <div ng-app="myApp" ng-controller="myCtrl">  
              
              <p>Select a car:</p>  
              
              <select ng-model="selectedCar" ng-options="x for (x, y) in cars">  
              
              </select>  
              
              <h1>You selected: {{selectedCar}}</h1>  
              
              </div>  
              
              <script>  
              
              var app = angular.module('myApp', []);  
              
              app.controller('myCtrl', function($scope) {  
              
                  $scope.cars = {  
              
                      car01 : "Ford",  
              
                      car02 : "Honda",  
              
                      car03 : "Volvo",  
              
                      car03 : "Hundai",  
              
                  }  
              
              });  
              
              </script>  
              
              </body>  
              
              </html>

              Example2:

              The selected value will always be the value in a key-value pair.

              The value in a key-value pair can also be an object:

              $scope.cars = {  
              
              car01 : {brand : "Ford", model : "Mustang", color : "red"},  
              
              car02 : {brand : "Honda", model : "city", color : "white"},  
              
              car03 : {brand : "Volvo", model : "XC90", color : "black"},  
              
              car04 : {brand : "Hundai", model : "Creta", color : "gray"}  
              
              }; 

                See this example:

                <!DOCTYPE html>  
                
                <html>  
                
                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                
                <body>  
                
                <div ng-app="myApp" ng-controller="myCtrl">  
                
                <p>Select a car:</p>  
                
                <select ng-model="selectedCar" ng-options="x for (x, y) in cars">  
                
                </select>  
                
                <h1>You selected: {{selectedCar.brand}}</h1>  
                
                <h2>Model: {{selectedCar.model}}</h2>  
                
                <h3>Color: {{selectedCar.color}}</h3>  
                
                </div>  
                
                <script>  
                
                var app = angular.module('myApp', []);  
                
                app.controller('myCtrl', function($scope) {  
                
                    $scope.cars = {  
                
                car01 : {brand : "Ford", model : "Mustang", color : "red"},  
                
                car02 : {brand : "Honda", model : "city", color : "white"},  
                
                car03 : {brand : "Volvo", model : "XC90", color : "black"},  
                
                car04 : {brand : "Hundai", model : "Creta", color : "gray"}  
                
                    }  
                
                });  
                
                </script>  
                
                </body>  
                
                </html>
              1. AngularJS Tables

                The ng-repeat directive is used to draw tables in AngularJS. Displaying tables with AngularJS is very easy and simple.

                Let’s take an example. This example use ng-repeat directive to draw a table.

                See this example:

                <table>  
                
                   <tr>  
                
                      <th>Name</th>  
                
                      <th>Marks</th>  
                
                   </tr>  
                
                   <tr ng-repeat = "subject in student.subjects">  
                
                      <td>{{ subject.name }}</td>  
                
                      <td>{{ subject.marks }}</td>  
                
                   </tr>  
                
                </table>

                Displaying with CSS style

                You can also style the tables by using CSS.

                See this example:

                <style>  
                
                   table, th , td {  
                
                      border: 1px solid grey;  
                
                      border-collapse: collapse;  
                
                      padding: 5px;  
                
                   }  
                
                     
                
                   table tr:nth-child(odd) {  
                
                      background-color: #f2f2f2;  
                
                   }  
                
                  
                
                   table tr:nth-child(even) {  
                
                      background-color: #ffffff;  
                
                   }  
                
                </style>

                AngularJS Table example with CSS

                <!DOCTYPE html>  
                
                <html>  
                
                   <head>  
                
                      <title>Angular JS Table</title>  
                
                      <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>  
                
                        
                
                      <style>  
                
                         table, th , td {  
                
                            border: 1px solid grey;  
                
                            border-collapse: collapse;  
                
                            padding: 5px;  
                
                         }  
                
                           
                
                         table tr:nth-child(odd) {  
                
                            background-color: #f2f2f2;  
                
                         }  
                
                           
                
                         table tr:nth-child(even) {  
                
                            background-color: #ffffff;  
                
                         }  
                
                      </style>  
                
                   </head>  
                
                   <body>  
                
                      <h2>AngularJS Sample Application</h2>  
                
                      <div ng-app = "mainApp" ng-controller = "studentController">  
                
                         <table border = "0">  
                
                            <tr>  
                
                               <td>Enter first name:</td>  
                
                               <td><input type = "text" ng-model = "student.firstName"></td>  
                
                            </tr>  
                
                            <tr>  
                
                               <td>Enter last name: </td>  
                
                               <td>  
                
                                  <input type = "text" ng-model = "student.lastName">  
                
                               </td>  
                
                            </tr>  
                
                            <tr>  
                
                               <td>Name: </td>  
                
                               <td>{{student.fullName()}}</td>  
                
                            </tr>  
                
                            <tr>  
                
                               <td>Subject:</td>  
                
                              <td>  
                
                                  <table>  
                
                                     <tr>  
                
                                        <th>Name</th>.  
                
                                        <th>Marks</th>  
                
                                     </tr>  
                
                                     <tr ng-repeat = "subject in student.subjects">  
                
                                        <td>{{ subject.name }}</td>  
                
                                        <td>{{ subject.marks }}</td>  
                
                                     </tr>  
                
                                  </table>  
                
                               </td>  
                
                                      
                
                            </tr>  
                
                         </table>  
                
                           
                
                      </div>  
                
                        
                
                      <script>  
                
                         var mainApp = angular.module("mainApp", []);  
                
                           
                
                         mainApp.controller('studentController', function($scope) {  
                
                            $scope.student = {  
                
                               firstName: "Rahul",  
                
                               lastName: "Rai",  
                
                               fees:500,  
                
                                 
                
                               subjects:[  
                
                                  {name:'Physics',marks:850},  
                
                                  {name:'Chemistry',marks:80},  
                
                                  {name:'Math',marks:90},  
                
                                  {name:'English',marks:80},  
                
                                  {name:'Hindi',marks:70}  
                
                               ],  
                
                                 
                
                               fullName: function() {  
                
                                  var studentObject;  
                
                                  studentObject = $scope.student;  
                
                                  return studentObject.firstName + " " + studentObject.lastName;  
                
                               }  
                
                            };  
                
                         });  
                
                      </script>  
                
                   </body>  
                
                </html>
              2. AngularJS Filters

                In AngularJS, filters are used to format data. Following is a list of filters used for transforming data.

                FilterDescription
                CurrencyIt formats a number to a currency format.
                DateIt formats a date to a specified format.
                FilterIt select a subset of items from an array.
                JsonIt formats an object to a Json string.
                LimitIt is used to limit an array/string, into a specified number of elements/characters.
                LowercaseIt formats a string to lower case.
                NumberIt formats a number to a string.
                OrderbyIt orders an array by an expression.
                UppercaseIt formats a string to upper case.

                How to add filters to expressions

                You can add filters to expressions by using the pipe character |, followed by a filter.

                In this example, the uppercase filter format strings to upper case:

                See this example:

                <!DOCTYPE html>  
                
                <html>  
                
                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                
                <body>  
                
                <div ng-app="myApp" ng-controller="personCtrl">  
                
                <p>The name is {{ firstName | uppercase }}</p>  
                
                </div>  
                
                <script>  
                
                angular.module('myApp', []).controller('personCtrl', function($scope) {  
                
                    $scope.firstName = "Sonoo",  
                
                    $scope.lastName = "Jaiswal"  
                
                });  
                
                </script>  
                
                </body>  
                
                </html>

                Let’s apply the lowercase filter into the same example:

                See this example:

                <!DOCTYPE html>  
                
                <html>  
                
                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                
                <body>  
                
                <div ng-app="myApp" ng-controller="personCtrl">  
                
                <p>The name is {{ firstName | lowercase }}</p>  
                
                </div>  
                
                <script>  
                
                angular.module('myApp', []).controller('personCtrl', function($scope) {  
                
                    $scope.firstName = "Sonoo",  
                
                    $scope.lastName = "Jaiswal"  
                
                });  
                
                </script>  
                
                </body>  
                
                </html>

                How to add filters to directives

                Filters can be added to directives, like ng-repeat, by using the pipe character |, followed by a filter.

                Let’s take an example:

                In this example, orderBy filter is used to sort an array.

                See this example:

                <!DOCTYPE html>  
                
                <html>  
                
                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                
                <body>      
                
                <div ng-app="myApp" ng-controller="namesCtrl">  
                
                <p>Looping with objects:</p>  
                
                <ul>  
                
                  <li ng-repeat="x in names | orderBy:'country'">  
                
                    {{ x.name + ', ' + x.country }}  
                
                  </li>  
                
                </ul>  
                
                </div>  
                
                <script>  
                
                angular.module('myApp', []).controller('namesCtrl', function($scope) {  
                
                    $scope.names = [  
                
                        {name:'Ramesh',country:'India'},  
                
                        {name:'Alex',country:'USA'},  
                
                        {name:'Pooja',country:'India'},  
                
                        {name:'Mahesh',country:'India'},  
                
                        {name:'Iqbal',country:'Pakistan'},  
                
                        {name:'Ramanujam',country:'India'},  
                
                        {name:'Osama',country:'Iraq'},  
                
                        {name:'Johnson',country:'UK'},  
                
                        {name:'Karl',country:'Russia'}  
                
                        ];  
                
                });  
                
                </script>  
                
                </body>  
                
                </html>

                The filter Filter

                The filter Filter can only be used on arrays because it selects a subset of an array. It returns an array containing only the matching items.

                Let’s take an example:

                This example will return the names that contain the letter “o”.

                See this example:

                <!DOCTYPE html>  
                
                <html>  
                
                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                
                <body>  
                
                <div ng-app="myApp" ng-controller="namesCtrl">  
                
                <ul>  
                
                  <li ng-repeat="x in names | filter : 'o'">  
                
                    {{ x }}  
                
                  </li>  
                
                </ul>  
                
                </div>  
                
                <script>  
                
                angular.module('myApp', []).controller('namesCtrl', function($scope) {  
                
                    $scope.names = [  
                
                'Ramesh',  
                
                'Pooja',  
                
                'Mahesh',  
                
                'Ramanujam',  
                
                'Osama',  
                
                'Iqbal',  
                
                'Karl',  
                
                'Johnson',  
                
                'Alex'  
                
                    ];  
                
                });  
                
                </script>  
                
                <p>This example displays only the names containing the letter "o".</p>  
                
                </body>  
                
                </html>

                Filter an array based on user input

                You can use the value of the input field as an expression in a filter by setting the ng-model directive on an input field.

                See this example:

                <!DOCTYPE html>  
                
                <html>  
                
                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                
                <body>  
                
                <div ng-app="myApp" ng-controller="namesCtrl">  
                
                <p>Type a letter in the input field:</p>  
                
                <p><input type="text" ng-model="test"></p>  
                
                <ul>  
                
                  <li ng-repeat="x in names | filter:test">  
                
                    {{ x }}  
                
                  </li>  
                
                </ul>  
                
                </div>  
                
                <script>  
                
                angular.module('myApp', []).controller('namesCtrl', function($scope) {  
                
                    $scope.names = [  
                
                        'Ramesh',  
                
                'Pooja',  
                
                'Mahesh',  
                
                'Ramanujam',  
                
                'Osama',  
                
                'Iqbal',  
                
                'Karl',  
                
                'Johnson',  
                
                'Alex'  
                
                   ];  
                
                });  
                
                </script>  
                
                <p>The list will only contain the names matching the filter.</p>  
                
                </body>  
                
                </html>

                Sort an array based on user input

                You can sort an array according to the table columns.

                See this example:

                <!DOCTYPE html>  
                
                <html>  
                
                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                
                <body>  
                
                <p>Click the table headers to change the sorting order:</p>  
                
                <div ng-app="myApp" ng-controller="namesCtrl">  
                
                <table border="1" width="100%">  
                
                <tr>  
                
                <th ng-click="orderByMe('name')">Name</th>  
                
                <th ng-click="orderByMe('country')">Country</th>  
                
                </tr>  
                
                <tr ng-repeat="x in names | orderBy:myOrderBy">  
                
                <td>{{x.name}}</td>  
                
                <td>{{x.country}}</td>  
                
                </tr>  
                
                </table>  
                
                </div>  
                
                <script>  
                
                angular.module('myApp', []).controller('namesCtrl', function($scope) {  
                
                       $scope.names = [  
                
                        {name:'Ramesh',country:'India'},  
                
                        {name:'Alex',country:'USA'},  
                
                        {name:'Pooja',country:'India'},  
                
                        {name:'Mahesh',country:'India'},  
                
                        {name:'Iqbal',country:'Pakistan'},  
                
                        {name:'Ramanujam',country:'India'},  
                
                        {name:'Osama',country:'Iraq'},  
                
                        {name:'Johnson',country:'UK'},  
                
                        {name:'Karl',country:'Russia'}  
                
                        ];  
                
                  
                
                    $scope.orderByMe = function(x) {  
                
                        $scope.myOrderBy = x;  
                
                    }  
                
                });  
                
                </script>  
                
                </body>  
                
                </html>

                AngularJS Custom Filters

                You can create your own filters by register a new filter factory function with your module.

                See this example: 

                <!DOCTYPE html>  <html>  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  <body>  <p>Click the table headers to change the sorting order:</p>  <div ng-app="myApp" ng-controller="namesCtrl">  <table border="1" width="100%">  <tr>  <th ng-click="orderByMe('name')">Name</th>  <th ng-click="orderByMe('country')">Country</th>  </tr>  <tr ng-repeat="x in names | orderBy:myOrderBy">  <td>{{x.name}}</td>  <td>{{x.country}}</td>  </tr>  </table>  </div>  <script>  angular.module('myApp', []).controller('namesCtrl', function($scope) {         $scope.names = [          {name:'Ramesh',country:'India'},          {name:'Alex',country:'USA'},          {name:'Pooja',country:'India'},          {name:'Mahesh',country:'India'},          {name:'Iqbal',country:'Pakistan'},          {name:'Ramanujam',country:'India'},          {name:'Osama',country:'Iraq'},          {name:'Johnson',country:'UK'},          {name:'Karl',country:'Russia'}          ];        $scope.orderByMe = function(x) {          $scope.myOrderBy = x;      }  });  </script>  </body>  </html> 
              3. AngularJS Dependency Injection

                AngularJS comes with a built-in dependency injection mechanism. It facilitates you to divide your application into multiple different types of components which can be injected into each other as dependencies.

                Dependency Injection is a software design pattern that specifies how components get holds of their dependencies. In this pattern, components are given their dependencies instead of coding them within the component.

                Modularizing your application makes it easier to reuse, configure and test the components in your application. Following are the core types of objects and components:

                • value
                • factory
                • service
                • provider
                • constant

                These objects and components can be injected into each other using AngularJS Dependency Injection.

                Value

                In AngularJS, value is a simple object. It can be a number, string or JavaScript object. It is used to pass values in factories, services or controllers during run and config phase.

                //define a module  
                
                var myModule = angular.module("myModule", []);  
                
                //create a value object and pass it a data.   
                
                myModule.value("numberValue", 100);  
                
                myModule.value("stringValue", "abc");  
                
                myModule.value("objectValue", { val1 : 123, val2 : "abc"} ); 

                  Here, values are defined using the value() function on the module. The first parameter specifies the name of the value, and the second parameter is the value itself. Factories, services and controllers can now reference these values by their name.

                  Injecting a value

                  To inject a value into AngularJS controller function, add a parameter with the same when the value is defined.

                  var myModule = angular.module("myModule", []);  
                  
                  myModule.value("numberValue", 100);  
                  
                  myModule.controller("MyController", function($scope, numberValue) {  
                  
                   console.log(numberValue);  
                  
                  });

                  Factory

                  Factory is a function that is used to return value. When a service or controller needs a value injected from the factory, it creates the value on demand. It normally uses a factory function to calculate and return the value.

                  Let’s take an example that defines a factory on a module, and a controller which gets the factory created value injected:

                   

                    Injecting values into factory

                    To inject a value into AngularJS controller function, add a parameter with the same when the value is defined.

                    var myModule = angular.module("myModule", []);  
                    
                    myModule.value("numberValue", 100);  
                    
                    myModule.controller("MyController", function($scope, numberValue) {  
                    
                     console.log(numberValue);  
                    
                    });

                    Note: It is not the factory function that is injected, but the value produced by the factory function.

                    Service

                    In AngularJS, service is a JavaScript object which contains a set of functions to perform certain tasks. Services are created by using service() function on a module and then injected into controllers.

                    //define a module  
                    
                    var mainApp = angular.module("mainApp", []);  
                    
                    ...  
                    
                    //create a service which defines a method square to return square of a number.  
                    
                    mainApp.service('CalcService', function(MathService){  
                    
                       this.square = function(a) {  
                    
                          return MathService.multiply(a,a);   
                    
                       }  
                    
                    });  
                    
                    //inject the service "CalcService" into the controller  
                    
                    mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {  
                    
                       $scope.number = defaultInput;  
                    
                       $scope.result = CalcService.square($scope.number);  
                    
                        $scope.square = function() {  
                    
                          $scope.result = CalcService.square($scope.number);  
                    
                       }  
                    
                    });

                    Provider

                    In AngularJS, provider is used internally to create services, factory etc. during config phase (phase during which AngularJS bootstraps itself). It is the most flexible form of factory you can create. Provider is a special factory method with a get() function which is used to return the value/service/factory.

                    //define a module  
                    
                    var mainApp = angular.module("mainApp", []);  
                    
                    ...  
                    
                    //create a service using provider which defines a method square to return square of a number.  
                    
                    mainApp.config(function($provide) {  
                    
                       $provide.provider('MathService', function() {  
                    
                          this.$get = function() {  
                    
                             var factory = {};    
                    
                             factory.multiply = function(a, b) {  
                    
                                return a * b;   
                    
                             }  
                    
                             return factory;  
                    
                          };  
                    
                       });  
                    
                    });

                    Constants

                    You cannot inject values into the module.config() function. Instead constants are used to pass values at config phase.

                    mainApp.constant("configParam", "constant value");   

                    Let’s take an example to deploy all above mentioned directives.

                    <!DOCTYPE html>  
                    
                    <html>  
                    
                       <head>  
                    
                          <title>AngularJS Dependency Injection</title>  
                    
                       </head>  
                    
                       <body>  
                    
                          <h2>AngularJS Sample Application</h2>  
                    
                            
                    
                          <div ng-app = "mainApp" ng-controller = "CalcController">  
                    
                             <p>Enter a number: <input type = "number" ng-model = "number" /></p>  
                    
                             <button ng-click = "square()">X<sup>2</sup></button>  
                    
                             <p>Result: {{result}}</p>  
                    
                          </div>  
                    
                            
                    
                          <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>  
                    
                            
                    
                          <script>  
                    
                             var mainApp = angular.module("mainApp", []);  
                    
                               
                    
                             mainApp.config(function($provide) {  
                    
                                $provide.provider('MathService', function() {  
                    
                                   this.$get = function() {  
                    
                                      var factory = {};  
                    
                                        
                    
                                      factory.multiply = function(a, b) {  
                    
                                         return a * b;  
                    
                                      }  
                    
                                      return factory;  
                    
                                   };  
                    
                                });  
                    
                             });  
                    
                                  
                    
                             mainApp.value("defaultInput", 10);  
                    
                               
                    
                             mainApp.factory('MathService', function() {  
                    
                                var factory = {};  
                    
                                  
                    
                                factory.multiply = function(a, b) {  
                    
                                   return a * b;  
                    
                                }  
                    
                                return factory;  
                    
                             });  
                    
                               
                    
                             mainApp.service('CalcService', function(MathService){  
                    
                                this.square = function(a) {  
                    
                                   return MathService.multiply(a,a);  
                    
                                }  
                    
                             });  
                    
                               
                    
                             mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {  
                    
                                $scope.number = defaultInput;  
                    
                                $scope.result = CalcService.square($scope.number);  
                    
                      
                    
                                $scope.square = function() {  
                    
                                   $scope.result = CalcService.square($scope.number);  
                    
                                }  
                    
                             });  
                    
                              </script>  
                    
                          </body>  
                    
                    </html>

                  1. AngularJS Scopes

                    The Scope is an object that is specified as a binding part between the HTML (view) and the JavaScript (controller). It plays a role of joining controller with the views. It is available for both the view and the controller.

                    How to use Scope

                    To make a controller in AngularJS, you have to pass the $scope object as an argument.

                    See this example:

                    <!DOCTYPE html>  
                    
                    <html>  
                    
                    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                    
                    <body>  
                    
                    <div ng-app="myApp" ng-controller="myCtrl">  
                    
                    <h1>{{carname}}</h1>  
                    
                    </div>  
                    
                    <script>  
                    
                    var app = angular.module('myApp', []);  
                    
                    app.controller('myCtrl', function($scope) {  
                    
                        $scope.carname = "Volvo";  
                    
                    });  
                    
                    </script>  
                    
                    <p>The property "carname" was made in the controller, and can be referred to in the view by using the {{ }} brackets.</p>  
                    
                    </body>  
                    
                    </html>
                  2. AngularJS Module

                    In AngularJS, a module defines an application. It is a container for the different parts of your application like controller, services, filters, directives etc.

                    A module is used as a Main() method. Controller always belongs to a module.

                    How to create a module

                    The angular object’s module() method is used to create a module. It is also called AngularJS function angular.module

                    <div ng-app="myApp">...</div>  
                    
                    <script>  
                    
                    var app = angular.module("myApp", []);   
                    
                    </script> 

                      Here, “myApp” specifies an HTML element in which the application will run.

                      Now we can add controllers, directives, filters, and more, to AngularJS application.

                      How to add controller to a module

                      If you want to add a controller to your application refer to the controller with the ng-controller directive.

                      See this example:

                      <!DOCTYPE html>  
                      
                      <html>  
                      
                      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                      
                      <body>  
                      
                      <div ng-app="myApp" ng-controller="myCtrl">  
                      
                      {{ firstName + " " + lastName }}  
                      
                      </div>  
                      
                      <script>  
                      
                      var app = angular.module("myApp", []);  
                      
                      app.controller("myCtrl", function($scope) {  
                      
                          $scope.firstName = "Ajeet";  
                      
                          $scope.lastName = "Maurya";  
                      
                      });  
                      
                      </script>  
                      
                      </body>  
                      
                      </html>

                      How to add directive to a module

                      AnglarJS directives are used to add functionality to your application. You can also add your own directives for your applications.

                      Following is a list of AngularJS directives:

                      DirectiveDescription
                      ng-appIt defines the root element of an application.
                      ng-bindIt binds the content of an html element to application data.
                      ng-bind-htmlItbinds the innerhtml of an html element to application data, and also removes dangerous code from the html string.
                      ng-bind-templateIt specifies that the text content should be replaced with a template.
                      ng-blurIt specifies a behavior on blur events.
                      ng-changeIt specifies an expression to evaluate when content is being changed by the user.
                      ng-checkedIt specifies if an element is checked or not.
                      ng-classIt specifies css classes on html elements.
                      ng-class-evenIt is same as ng-class, but will only take effect on even rows.
                      ng-class-oddIt is same as ng-class, but will only take effect on odd rows.
                      ng-clickIt specifies an expression to evaluate when an element is being clicked.
                      ng-cloakIt prevents flickering when your application is being loaded.
                      ng-controllerIt defines the controller object for an application.
                      ng-copyIt specifies a behavior on copy events.
                      ng-cspIt changes the content security policy.
                      ng-cutIt specifies a behavior on cut events.
                      ng-dblclickIt specifies a behavior on double-click events.
                      ng-disabledIt specifies if an element is disabled or not.
                      ng-focusIt specifies a behavior on focus events.
                      ng-formIt specifies an html form to inherit controls from.
                      ng-hideIt hides or shows html elements.
                      ng-hrefIt specifies a URL for the <a> element.
                      ng-ifIt removes the html element if a condition is false.
                      ng-includeIt includes html in an application.
                      ng-initIt defines initial values for an application.
                      ng-jqIt specifies that the application must use a library, like jQuery.
                      ng-keydownIt specifies a behavior on keydown events.
                      ng-keypressIt specifies a behavior on keypress events.
                      ng-keyupIt specifies a behavior on keyup events.
                      ng-listIt converts text into a list (array).
                      ng-modelIt binds the value of html controls to application data.
                      ng-model-optionsIt specifies how updates in the model are done.
                      ng-mousedownIt specifies a behavior on mousedown events.
                      ng-mouseenterIt specifies a behavior on mouseenter events.
                      ng-mouseleaveIt specifies a behavior on mouseleave events.
                      ng-mousemoveIt specifies a behavior on mousemove events.
                      ng-mouseoverIt specifies a behavior on mouseover events.
                      ng-mouseupIt specifies a behavior on mouseup events.
                      ng-non-bindableIt specifies that no data binding can happen in this element, or it’s children.
                      ng-openIt specifies the open attribute of an element.
                      ng-optionsIt specifies <options> in a <select> list.
                      ng-pasteIt specifies a behavior on paste events.
                      ng-pluralizeIt specifies a message to display according to en-us localization rules.
                      ng-readonlyIt specifies the readonly attribute of an element.
                      ng-repeatIt defines a template for each data in a collection.
                      ng-requiredIt specifies the required attribute of an element.
                      ng-selectedIt specifies the selected attribute of an element.
                      ng-showIt shows or hides html elements.
                      ng-srcIt specifies the src attribute for the <img> element.
                      ng-srcsetIt specifies the srcset attribute for the <img> element.
                      ng-styleIt specifies the style attribute for an element.
                      ng-submitIt specifies expressions to run on onsubmit events.
                      ng-switchIt specifies a condition that will be used to show/hide child elements.
                      ng-transcludeIt specifies a point to insert transcluded elements.
                      ng-valueIt specifies the value of an input element.

                      How to add directives

                      See this example:

                      <!DOCTYPE html>  
                      
                      <html>  
                      
                      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                      
                      <body>  
                      
                        
                      
                      <div ng-app="myApp" w3-test-directive></div>  
                      
                      <script>  
                      
                      var app = angular.module("myApp", []);  
                      
                      app.directive("w3TestDirective", function() {  
                      
                          return {  
                      
                              template : "This is a directive constructor. "  
                      
                          };  
                      
                      });  
                      
                      </script>  
                      
                      </body>  
                      
                      </html>

                      Modules and controllers in file

                      In AngularJS applications, you can 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:

                      See this example:

                      <!DOCTYPE html>  
                      
                      <html>  
                      
                      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                      
                      <body>  
                      
                      <script src="myApp.js"></script>  
                      
                      <script src="myCtrl.js"></script>  
                      
                      </body>  
                      
                      </html> 

                        Here “myApp.js” contains:

                          

                          Here “myCtrl.js” contains:

                           <div ng-app="myApp" ng-controller="myCtrl">  
                          
                          {{ firstName + " " + lastName }}  
                          
                          </div>

                            This example can also be written as:

                            <!DOCTYPE html>  
                            
                            <html>  
                            
                            <body>  
                            
                            <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                            
                            <div ng-app="myApp" ng-controller="myCtrl">  
                            
                            {{ firstName + " " + lastName }}  
                            
                            </div>  
                            
                            <script>  
                            
                            var app = angular.module("myApp", []);  
                            
                            app.controller("myCtrl", function($scope) {  
                            
                                $scope.firstName = "Ajeet";  
                            
                                $scope.lastName = "Maurya";  
                            
                            });  
                            
                            </script>  
                            
                            </body>  
                            
                            </html>
                          1. AngularJS Controllers

                            AngularJS controllers are used to control the flow of data of AngularJS application. A controller is defined using ng-controller directive. A controller is a JavaScript object containing attributes/properties and functions. Each controller accepts $scope as a parameter which refers to the application/module that controller is to control.

                            AngularJS Controller Example

                            <!DOCTYPE html>  
                            
                            <html>  
                            
                            <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                            
                            <body>  
                            
                              
                            
                            <div ng-app="myApp" ng-controller="myCtrl">  
                            
                              
                            
                            First Name: <input type="text" ng-model="firstName"><br>  
                            
                            Last Name: <input type="text" ng-model="lastName"><br>  
                            
                            <br>  
                            
                            Full Name: {{firstName + " " + lastName}}  
                            
                              
                            
                            </div>  
                            
                              
                            
                            <script>  
                            
                            var app = angular.module('myApp', []);  
                            
                            app.controller('myCtrl', function($scope) {  
                            
                                $scope.firstName = "Aryan";  
                            
                                $scope.lastName = "Khanna";  
                            
                            });  
                            
                            </script>  
                            
                              
                            
                            </body>  
                            
                            </html>

                            Note:

                            • Here, the AngularJS application runs inside the <div> is defined by ng-app=”myApp”.
                            • The AngularJS directive is ng-controller=”myCtrl” attribute.
                            • The myCtrl function is a JavaScript function.
                            • AngularJS will invoke the controller with a $scope object.
                            • In AngularJS, $scope is the application object (the owner of application variables and functions).
                            • The controller creates two properties (variables) in the scope (firstName and lastName).
                            • The ng-model directives bind the input fields to the controller properties (firstName and lastName).

                            AngularJS controller example with methods (variables as functions)

                            <!DOCTYPE html>  
                            
                            <html>  
                            
                            <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                            
                            <body>  
                            
                              
                            
                            <div ng-app="myApp" ng-controller="personCtrl">  
                            
                              
                            
                            First Name: <input type="text" ng-model="firstName"><br>  
                            
                            Last Name: <input type="text" ng-model="lastName"><br>  
                            
                            <br>  
                            
                            Full Name: {{fullName()}}  
                            
                              
                            
                            </div>  
                            
                            <script>  
                            
                            var app = angular.module('myApp', []);  
                            
                            app.controller('personCtrl', function($scope) {  
                            
                                $scope.firstName = "Aryan";  
                            
                                $scope.lastName = "Khanna";  
                            
                                $scope.fullName = function() {  
                            
                                    return $scope.firstName + " " + $scope.lastName;  
                            
                                };  
                            
                            });  
                            
                            </script>  
                            
                              
                            
                            </body>  
                            
                            </html>

                            AngularJS Controller in external files

                            In larger applications, generally the controllers are stored in external files.

                            Create an external file named “personController.js” to store controller.

                            Here, “personController.js” is:

                            angular.module('myApp', []).controller('personCtrl', function($scope) {  
                            
                                $scope.firstName = "Aryan",  
                            
                                $scope.lastName = "Khanna",  
                            
                                $scope.fullName = function() {  
                            
                                    return $scope.firstName + " " + $scope.lastName;  
                            
                                }  
                            
                            }); 

                              See this example:

                              <!DOCTYPE html>  
                              
                              <html>  
                              
                              <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                              
                              <body>  
                              
                              <div ng-app="myApp" ng-controller="personCtrl">  
                              
                              First Name: <input type="text" ng-model="firstName"><br>  
                              
                              Last Name: <input type="text" ng-model="lastName"><br>  
                              
                              <br>  
                              
                              Full Name: {{firstName + " " + lastName}}  
                              
                              </div>  
                              
                              <script src="personController.js"></script>  
                              
                              </body>  
                              
                              </html>
                            1. AngularJS Directives

                              AngularJS facilitates you to extend HTML with new attributes. These attributes are called directives.

                              There is a set of built-in directive in AngularJS which offers functionality to your applications. You can also define your own directives.

                              Directives are special attributes starting with ng- prefix. Following are the most common directives:

                              • ng-app: This directive starts an AngularJS Application.
                              • ng-init: This directive initializes application data.
                              • ng-model: This directive defines the model that is variable to be used in AngularJS.
                              • ng-repeat: This directive repeats html elements for each item in a collection.

                              ng-app directive

                              ng-app directive defines the root element. It starts an AngularJS Application and automatically initializes or bootstraps the application when web page containing AngularJS Application is loaded. It is also used to load various AngularJS modules in AngularJS Application.

                              See this example:

                              In following example, we’ve defined a default AngularJS application using ng-app attribute of a div element.

                              <div ng-app = "">  
                              
                                 ...  
                              
                              </div>

                              ng-init directive

                              ng-init directive initializes an AngularJS Application data. It defines the initial values for an AngularJS application.

                              In following example, we’ll initialize an array of countries. We’re using JSON syntax to define array of countries.

                              <div ng-app = "" ng-init = "countries = [{locale:'en-IND',name:'India'}, {locale:'en-PAK',name:'Pakistan'}, {locale:'en-AUS',name:'Australia'}]">  
                              
                                 ...  
                              
                              </div>

                              ng-model directive:

                              ng-model directive defines the model/variable to be used in AngularJS Application.

                              In following example, we’ve defined a model named “name”.

                              <div ng-app = "">  
                              
                                 ...  
                              
                                 <p>Enter your Name: <input type = "text" ng-model = "name"></p>  
                              
                              </div>

                              ng-repeat directive

                              ng-repeat directive repeats html elements for each item in a collection. In following example, we’ve iterated over array of countries.

                              <div ng-app = "">  
                              
                                 ...  
                              
                                 <p>List of Countries with locale:</p>  
                              
                                   
                              
                                 <ol>  
                              
                                    <li ng-repeat = "country in countries">  
                              
                                       {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}  
                              
                                    </li>  
                              
                                 </ol> 

                                AngularJS directives Example

                                Let’s take an example to use all the above discussed directives:

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <head>  
                                
                                      <title>AngularJS Directives</title>  
                                
                                </head>  
                                
                                <body>  
                                
                                      <h1>Sample Application</h1>  
                                
                                        
                                
                                      <div ng-app = "" ng-init = "countries = [{locale:'en-IND',name:'India'}, {locale:'en-PAK',name:'Pakistan'}, {locale:'en-AUS',name:'Australia'}]">   
                                
                                         <p>Enter your Name: <input type = "text" ng-model = "name"></p>  
                                
                                         <p>Hello <span ng-bind = "name"></span>!</p>  
                                
                                         <p>List of Countries with locale:</p>  
                                
                                        
                                
                                         <ol>  
                                
                                            <li ng-repeat = "country in countries">  
                                
                                               {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}  
                                
                                            </li>  
                                
                                         </ol>  
                                
                                      </div>  
                                
                                <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>  
                                
                                </body>  
                                
                                </html>

                                AngularJS Directives List

                                AnglarJS directives are used to add functionality to your application. You can also add your own directives for your applications.

                                Following is a list of AngularJS directives:

                                DirectiveDescription
                                ng-appIt defines the root element of an application.
                                ng-bindIt binds the content of an html element to application data.
                                ng-bind-htmlIt binds the inner HTML of an HTML element to application data, and also removes dangerous code from the html string.
                                ng-bind-templateIt specifies that the text content should be replaced with a template.
                                ng-blurIt specifies a behavior on blur events.
                                ng-changeIt specifies an expression to evaluate when content is being changed by the user.
                                ng-checkedIt specifies if an element is checked or not.
                                ng-classIt specifies css classes on html elements.
                                ng-class-evenIt is same as ng-class, but will only take effect on even rows.
                                ng-class-oddIt is same as ng-class, but will only take effect on odd rows.
                                ng-clickIt specifies an expression to evaluate when an element is being clicked.
                                ng-cloakIt prevents flickering when your application is being loaded.
                                ng-controllerIt defines the controller object for an application.
                                ng-copyIt specifies a behavior on copy events.
                                ng-cspIt changes the content security policy.
                                ng-cutIt specifies a behavior on cut events.
                                ng-dblclickIt specifies a behavior on double-click events.
                                ng-focusIt specifies a behavior on focus events.
                                ng-hideIt hides or shows html elements.
                                ng-hrefIt specifies a URL for the <a> element.
                                ng-ifIt removes the html element if a condition is false.
                                ng-includeIt includes html in an application.
                                ng-initIt defines initial values for an application.
                                ng-jqIt specifies that the application must use a library, like jQuery.
                                ng-keydownIt specifies a behavior on keydown events.
                                ng-keypressIt specifies a behavior on keypress events.
                                ng-keyupIt specifies a behavior on keyup events.
                                ng-listIt converts text into a list (array).
                                ng-openIt specifies the open attribute of an element.
                                ng-optionsIt specifies <options> in a <select> list.
                                ng-pasteIt specifies a behavior on paste events.
                                ng-pluralizeIt specifies a message to display according to en-us localization rules.
                                ng-readonlyIt specifies the readonly attribute of an element.
                                ng-requiredIt specifies the required attribute of an element.
                                ng-selectedIt specifies the selected attribute of an element.
                                ng-showIt shows or hides html elements.
                                ng-srcIt specifies the src attribute for the <img> element.
                                ng-srcsetIt specifies the srcset attribute for the <img> element.
                                ng-styleIt specifies the style attribute for an element.
                                ng-submitIt specifies expressions to run on onsubmit events.
                                ng-switchIt specifies a condition that will be used to show/hide child elements.
                                ng-transcludeIt specifies a point to insert transcluded elements.
                                ng-valueIt specifies the value of an input element.
                                ng-disabledIt specifies if an element is disabled or not.
                                ng-formIt specifies an html form to inherit controls from.
                                ng-modelIt binds the value of html controls to application data.
                                ng-model-optionsIt specifies how updates in the model are done.
                                ng-mousedownIt specifies a behavior on mousedown events.
                                ng-mouseenterIt specifies a behavior on mouseenter events.
                                ng-mouseleaveIt specifies a behavior on mouseleave events.
                                ng-mousemoveIt specifies a behavior on mousemove events.
                                ng-mouseoverIt specifies a behavior on mouseover events.
                                ng-mouseupIt specifies a behavior on mouseup events.
                                ng-non-bindableIt specifies that no data binding can happen in this element, or it’s children.
                                ng-repeatIt defines a template for each data in a collection.

                                How to add directives

                                See this example:

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                  
                                
                                <div ng-app="myApp" w3-test-directive></div>  
                                
                                <script>  
                                
                                var app = angular.module("myApp", []);  
                                
                                app.directive("w3TestDirective", function() {  
                                
                                    return {  
                                
                                        template : "This is a directive constructor. "  
                                
                                    };  
                                
                                });  
                                
                                </script>  
                                
                                </body>  
                                
                                </html>
                              1. AngularJS Expressions

                                In AngularJS, expressions are used to bind application data to HTML. AngularJS resolves the expression, and return the result exactly where the expression is written.

                                Expressions are written inside double braces {{expression}}.They can also be written inside a directive:

                                ng-bind="expression".  

                                AnularJS expressions are very similar to JavaScript expressions. They can contain literals, operators, and variables. For example:

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

                                AngularJS Expressions Example

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app>  
                                
                                <p>A simple expression example: {{ 5 + 5 }}</p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                Note: If you remove the directive “ng-app”, HTML will display the expression without solving it.

                                See this example:

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <p>If you remove the directive "ng-app", HTML will display the expression without solving it.</p>  
                                
                                <div>  
                                
                                <p>A simple expression example:  {{ 5 + 5 }}</p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html> 

                                You can also write expressions wherever you want, AngularJS will resolve the expression and return the result.

                                Let’s take an example to change the color of input box by changing its value.

                                See this example:

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <p>Change the value of the input field:</p>  
                                
                                <div ng-app="" ng-init="myCol='pink'">  
                                
                                <input style="background-color:{{myCol}}" ng-model="myCol" value="{{myCol}}">  
                                
                                </div>  
                                
                                <p>AngularJS resolves the expression and returns the result.</p>  
                                
                                <p>The background color of the input box will be whatever you write in the input field.</p>  
                                
                                </body>  
                                
                                </html>

                                AngularJS Numbers

                                AngularJS numbers are similar to JavaScript numbers.

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app="" ng-init="quantity=5;cost=5">  
                                
                                <p>Total in dollar: {{ quantity * cost }}</p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                We can use the same example by using ng-bind:

                                See this example:

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app="" ng-init="quantity=5;cost=5">  
                                
                                <p>Total in dollar: <span ng-bind="quantity * cost"></span></p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                AngularJS Strings

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app="" ng-init="firstName='Sonoo';lastName='Jaiswal'">  
                                
                                <p>My full name is: {{ firstName + " " + lastName }}</p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                Same example with ng-bind:

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app="" ng-init="firstName='Sonoo';lastName='Jaiswal'">  
                                
                                <p>My full name is: <span ng-bind="firstName + ' ' + lastName"></span></p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                AngularJS Objects

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app="" ng-init="person={firstName:'Sonoo',lastName:'Jaiswal'}">  
                                
                                <p>My name is {{ person.firstName }}</p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                Same example with ng-bind:

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app="" ng-init="person={firstName:'Sonoo',lastName:'Jaiswal'}">  
                                
                                <p>The name is <span ng-bind="person.firstName"></span></p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                AngularJS Arrays

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app="" ng-init="points=[1,15,19,2,40]">  
                                
                                <p>The first result is {{ points[0] }}</p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                Same example with ng-bind:

                                <!DOCTYPE html>  
                                
                                <html>  
                                
                                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
                                
                                <body>  
                                
                                <div ng-app="" ng-init="points=[1,15,19,2,40]">  
                                
                                <p>The first result is <span ng-bind="points[0]"></span></p>  
                                
                                </div>  
                                
                                </body>  
                                
                                </html>

                                Difference between AngularJS Expressions and JavaScript expressions:

                                • AngularJS expressions can be written inside HTML, while JavaScript expressions cannot.
                                • AngularJS expressions support filters, while JavaScript expressions do not.
                                • AngularJS expressions do not support conditionals, loops, and exceptions, while JavaScript expressions do.

                                Similarity between AngularJS Expressions and JavaScript expressions:

                                • AngularJS expressions and JavaScript expressions both can contain literals, operators and variables.