Showing posts with label Angular JS. Show all posts
Showing posts with label Angular JS. Show all posts

Friday, 5 June 2015

Angular JS Expressions,Controllers and Filters


In my last article I discussed about Angular JS and its Directives. You may checked it here Introduction to Angular JS and Angular JS Directives.




How to use Expressions,Controllers and Filters in Angular JS.


Expressions:

  1. Angular JS expressions are written under {{ expression}}
  2. It bind the data same as ng-bind directive.
  3. It returns the OUTPUT exactly where the expression is written. 
Example:


<!DOCTYPE html>

<html>

<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<body>

<div ng-app="" ng-init="FName='Ashraf'; LName='Ali'">

<p>The full name is: {{ FName + " " + LName }}</p>//Expression written here

</div>

</body>
</html>


Controllers:

  1. Angular JS applications are controlled by controllers. 
  2. ng-controller directives is used to create angular applications.
Example:

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

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="FName"><br>
Last Name: <input type="text" ng-model="LName"><br>
Address: <input type="text" ng-model="Address"><br>
<br>
Full Name: {{FName + " " + LName}}
Address :{{Address}}
</div>

<script>

//Controller written here
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.FName = "Ashraf";
    $scope.LName = "Ali";
    $scope.Address="Delhi";
});
</script>

</body>
</html>

Filters:

  1. Filters are used in Angular js expression with pipe(|) character. 
Example:

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

<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>
</table>
<br/>
<table border="0">
<tr><td>Name in Upper Case: </td><td>{{student.fullName() | uppercase}}</td></tr>
<tr><td>Name in Lower Case: </td><td>{{student.fullName() | lowercase}}</td></tr>


</table>
</div>
<script>
var mainApp = angular.module("mainApp", []);

mainApp.controller('studentController', function($scope) {
   $scope.student = {
      firstName: "Ashraf",
      lastName: "Ali",
      
      fullName: function() {
         var studentObject;
         studentObject = $scope.student;
         return studentObject.firstName + " " + studentObject.lastName;
      }
   };
});
</script>

<<PREVIOUS                                                                                    

Thursday, 4 June 2015

Directives of Angular JS



In my last article I tried to Introduce you about Angular JS. You may checked it here Introduction to Angular JS.




Directives available in Angular JS..

1.ng-app - This directive starts an AngularJS Application.

Example:


<!DOCTYPE html>

<html>

<script 

src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<body>


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

</body>

</html>




2.ng-init - This directive initializes application data.

Example:

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

<div ng-app="" ng-init="Name='Ashraf'">

<p>Input something in the input box:</p>
<p>Name: <input type="text" ng-model="Name"></p>
<p>You wrote: {{ Name }}</p>

</div>

</body>
</html>
3.ng-model - This directive defines the model that is variable to be used in AngularJS.

Example:


<!DOCTYPE html>

<html>

<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<body>



<div data-ng-app="" data-ng-init="quantity=1;price=5">

<h2>Cost Calculator</h2>

Quantity: <input type="number" ng-model="quantity">
Price: <input type="number" ng-model="price">

<p><b>Total in dollar:</b> {{quantity * price}}</p>

</div>

</body>
</html>

4.ng-repeat - This directive repeats html elements for each item in a collection.

Example:



<!DOCTYPE html>

<html>

<script

 src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<body>


<div ng-app="" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">

<p>Looping with objects:</p>
<ul>
  <li ng-repeat="x in names">
  {{ x.name + ', ' + x.country }}</li>
</ul>

</div>

</body>
</html>

<<PREVIOUS                                                                                 NEXT>>

Introduction to Angular JS

Basics of Angular JS 



WHAT IS ANGULAR JS?

AngularJS is a very powerful JavaScript library. It is used in Single Page Application (SPA) projects. It extends HTML DOM with additional attributes and makes it more responsive to user actions. 

FEATURES:
  1. AngularJS is a powerful JavaScript based development framework to create RICH Internet Application(RIA).
  2. AngulajJS provides developers options to write client side application (using JavaScript) in a clean MVC(Model View Controller) way.
  3. Application written in AngularJS is cross-browser compliant. AngularJS automatically handles javascript code suitable for each browser.


      Some Important parts of Angular JS-
        AngularJS Concepts


        Following are most important core features of AngularJS:
        Data-binding: It is the automatic synchronization of data between model and view components.

        Scope: These are objects that refer to the model. They act as a glue between controller and view.

        Controller: These are Javascript functions that are bound to a particular scope.

        Services: AngularJS come with several built-in services for example $http to make a XMLHttpRequests. These are singleton objects which are instantiated only once in app.

        Filters: These select a subset of items from an array and returns a new array.

        Directives: Directives are markers on DOM elements (such as elements, attributes, css, and more). These can be used to create custom HTML tags that serve as new, custom widgets. AngularJS has built-in directives (ngBind, ngModel...)

        Templates:These are the rendered view with information from the controller and model. These can be a single file (like index.html) or multiple views in one page using "partials".

        Routing: It is concept of switching views.

        Model View Whatever: MVC is a design pattern for dividing an application into different parts (called Model, View and Controller), each with distinct responsibilities. AngularJS does not implement MVC in the traditional sense, but rather something closer to MVVM (Model-View-ViewModel). The Angular JS team refers it humorously as Model View Whatever.

        Deep Linking: Deep linking allows you to encode the state of application in the URL so that it can be bookmarked. The application can then be restored from the URL to the same state.

        Dependency Injection: AngularJS has a built-in dependency injection subsystem that helps the developer by making the application easier to develop, understand, and test.

        Reference : http://www.tutorialspoint.com/
        Join US Our Community
        ×