Angular JS

Angular JS

Angular JS is a Jawa script framework that extend HTML with new attributes. Angular JS is perfect for single phase applications. To mark an HTML element as a container for an angular JS application, write the attribute NG-app.

For example
<div ng-app=""> Angular JS can be added to an HTML page with a <script> tags. Angular JS has attributes called directives. The ng-model directive binds the value of HTML input control to application data. The ng-bind directive binds application data to the HTML.

Angular JS is a MVVM (Model View View Model) framework because it binds MOdel to teh VIew and View to the model using ng-model and ng-bind

  
 <div ng-app="gaurav">
 <p> Name:
 <input type="text" ng-model="name" />
  </p>
  <p ng-bind="name"></p>
  </div>

  <div ng-app="shirodkar" ng-init="firstName='John'">
    <p> The name is <span ng-bind="firstName"></span></p>
  </div>

Anugular JS expresions are written inside curly braces {{ expression }}.
Angular JS will "output" data exactly where the application is written.


 <div ng-app="Gaurav">
  <p>My first expression is: {{ 5 + 5 }}</p>
  </div>

  <div ng-app="">
    <p>Name:
    <input type="text" ng-model="name" />
      </p>
  <p>{{ name }} </p>
  </div>

Angular JS controllers provide the functionality whenever a event occurs.
Events may be anything like storing data, clicking of a button, updating $scopen object, listening to a url with or without query parameters, making AJAX calls and so on.


  <div ng-app="Gaurav">
  <'div ng-controller="gaurav-controller">
    </div>
  </div>

  let app = angular.module("Gaurav",[]);
  app.controller("gaurav-controller",
                 function($scope) {
    				$scope.firstName = "Gaurav";
    				$scope.lastName  = "Shirodkar";
    
  }
);

Inserting expressions in HTML


  <div ng-app="" ng-init="myCol='lightblue'">
  <input type="text" style="background-color:{{myCol}}" ng-model="myCol" />
   </div>

Angular JS numbers are like Java Script numbers


  <div ng-app="" ng-init="quantity=10;cost=5;">
  <p>Total in dollar: {{ quantity * cost }} 
  </p>
  </div>

Same example using ng-bind


  <div ng-app="" ng-init="quantity=10;cost=5">
    <p>Total in dollar: <span ng-bind='quantity * cost"></span></p>
  </div>

AngularJS strings


  <div ng-app="" ng-init="firstName='Gaurav';lastName='Shirodkar''>
  <p> The full name is {{ firstName + " " + lastName }}</p>
  </div>

Same example using ng-bind:


  <div ng-app="" ng-init="firstName='Gaurav';lastName='Shirodkar'">
  <p>The name is <span ng-bind="firstName + ' ' + lastName"></span>
  </div>

ANgular JS objects are like JAvascript objects.


  <div ng-app="" ng-init="person={firstName='Gaurav',lastname='Shirodkar'}">
  <p>The name is {{ person.lastName }}</p>


<div ng-app="" ng-init="quantity=1;price=5">
  Quantity: <input type="number" ng-model="quantity" />
  Costs: <input type="text" ng-model="price: />
    Total: {{ quantity * price }}

Repeating HTML elements: The ng-repeat directive repeats ann HTNLM element.

Example:
        
    <div ng-app="" ng-init="names=['Jani','Hege','Kai']">
  <ul>
    <li ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>

The ng-repeat directibve actually clones HTML elements oonce for each item in a collection. Thge ng-repeat used on an array of objects.

Example:

<div ng-app="" ng-init="name = { name : 'Jani', country : 'Norway'}">
{{ name: 'Hehe' country: 'Sweden'}},
{{name: 'KAi' , country: 'Denmark' }}

Two way binding:

The binding goes both ways: If the user changes trhe value inside the input field, the angularJS property will also change its value

Validate user input:

The ng-model directive can provide type validation for application data (number ,email,required);

CSS classes:

The ng-model directive provides CSS classes for HTML elements, depending on their states. The ng-model directive adds / removes the following classes, acording to the status ofthe form filled:
  • ng-empty
  • ng-notempty
  • ng-touched
  • ng-untouched
  • ng-valid
  • ng-invalid
  • ng-dirty
  • ng-pending
  • ng-pristine
                                                            
<div ng-app="myApp" ng-controller="myCtrl">
	<h1 ng-click="changeName()">
	{{ firstName }}
    </h1>       
</div>
<div>
  <script>
      var app = angular.module("myApp",[]);
      app.controller("myCtrl", function($scope) {
      $scope.firstName = "John";
      $scope.changeName = function() {
          $scope.firstName = "Nege";
          }
      }
      );
  </script>
</div>

Comments

Popular posts from this blog

Parallel Database design, query processing

Laravel | PHP | Basics | Part 2

Apache Hadoop | Running MapReduce Jobs