Showing posts with label AngularJS. Show all posts
Showing posts with label AngularJS. Show all posts

Saturday, 1 February 2014

Simple form using AngularJS


Form is a collection of controls for the purpose of grouping related controls together.

Form and controls provide validation services, so that the user can be notified of invalid input. This provides a better user experience, because the user gets instant feedback on how to correct the error. Keep in mind that while client-side validation plays an important role in providing good user experience, it can easily be circumvented and thus can not be trusted. Server-side validation is still necessary for a secure application.

The key directive in understanding two-way data-binding is ngModel. The ngModel directive provides the two-way data-binding by synchronizing the model to the view, as well as view to the model.

index.html

<!doctype html>
<html ng-app>
   <head>
         <script src="http://code.angularjs.org/1.0.4angular.min.js"></script>
         <script src="script.js"></script>
  </head>
  
<body>

  <form novalidate class="simple-form">

   Name:<input type="text" ng-model="user.name" /><br />

   E-Mail: <input type="email" ng-model="user.email" /><br />

   Gender <input type="radio" ng-model="user.gender" value="male" / >Male

   <input type="radion" ng-model="user.gender" value="female" />Female<br />

   <button ng-click="reset()">Reset</button>

   <button ng-click="update(user)">Save</button>

</form>

<pre>form = {{user | json}}</pre>

<pre>master = {{master | json}}</pre>

</div>

</body>

</html>

script.js

function controller($scope) {

$scope.master = {}'

$scope.update = function(user) {

$scope.master = angular.copy(user);

};

$scope.reset = function() {

$scope.user = angular.copy($scope.master);

};

$scope.reset();

}
Angular Expressions vs JS Expressions


It might be tempting to think of Angular view expressions as Javascript expressions, but that is not entirely correct, since angular does not use a Javascript eval() to evaluate expressions. You can think of Angular expressions as Javascript expressions with following diffences:

1.Attirbute Evaluation: 
                              
                             Evaluation of all properties are against the scope, doing the evaluation, unlike in Javascript where the expressions are evaluate against the global window.

2.Forgiving

             Expression evaluation is forgiving to undefined and null, unlike in Javascript, where such evaluations generate NullPointerExceptions.


3.No Control Flow Statements

             You can pass result of expression evaluations through filter chains. For example to convert date object into a local specific human-readable format.

If, on the other hand, you do want to run arbitrary Javascript code, you should make it a controller method and call the method. If you want to eval() an angular expression from JavaScript, use the $eval() method.

index.html

<!doctype html>
 <html ng-app>
     <head>
         <script src="http://code.angularjs.org/1.0.4angular.min.js"></script> 
     </head>
     <body>
               1+2={{1+2}}

     </body>       
 </html>

Result:

1+2 = 3

You can try evaluating different expressions here:

index.html

<!doctype html>
<html ng-app>
     <head>
           <script src="http://code.angularjs.org/1.0.4angular.min.js"></script>
           <script src="script.js"></script>
     </head>
     <body>
           <div ng-controller="expcnt" class="expressions">

            Expression:

            <input type="text" ng-model="expr" size="80" />

             <button ng-click="addExp(expr)">Evaluate</button>

              <ul>

                   <li ng-repeat="expr in exprs">

                     [<a href="" ng-click="removeExp($index)">X</a>]

                     <tt>{{expr}}</tt> => <span ng-bind="$parent.$eval(expr)"></span>
                    </li>

                 </ul>

               </div>

           </body>

        </html>


script.js

function expcnt($scope) {

var exprs = $scope.exprs = [];

$scope.expr = '3*10 | currency';

$scope.addExp = function(expr) {

exprs.push(expr);

};

$scope.removeExp  = function(index) {

 exprs.splice(index,1);

};

}

Saturday, 4 January 2014

                                                     How use fileters in angularjs


Filters perform data transformation. Typically they are used in conjunction with the locale to format the data in  locale specific output.

index.html

<!doctype html>

<html ng-app>

        <head>

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

        </head>

        <body>

                <div ng-init= "list = ['iPhone ', 'HTC','Samaung','Nokia']">
               
                 Number formatting : {{1234567890 | number}} <br>

                Array filtering <input ng-model="predicate">

                {{ list | filter:predicate | json}}

                </div>
        </body>
</html>
  


refer : jsfiddle work place


Wednesday, 27 November 2013

                                             How to connect angularjs with MySql database


Now, we are discusses about connect angularjs with backend. It is one of the way to connect database. AngularJS is one of the powerful web application framework. The angularjs is reducing the backend overload.

Understanding the angularjs concept  refer previous tutorials. Easy to understanding server loading data examples:

1.Without AngularJS Web application performance :


First diagram, without using angularjs concept  building web applications diagram. This diagram explains, server workload high and  performance low and response time is very high.


2. With AngularJS web application performance:


Second diagram, building web applications with angularjs - server work load very low and performance very high and response time is very quick processes.

The angularjs is get all data from database and its make JSON(Java Script Object Notation) format.

The connecting angularjs with mysql database.

 First method call to controller site.



sample code :



$scope.addBook = function()
{
http({

method : 'POST',
url: 'index.php?urls',
data: {book_name : $scope.bookName, book_price : $scope.bookPrice},
headers : {'Content-type' : 'application/json'}
}).success(function(data, status, headers, config){

if(data.success)
{
$scope.books.push(data);
 }

}).error(function(data, status, headers, config){

//set error message.

});

}


addBook() is one of the method in view site. The addBook() method call to controller site.

ex:

<button ng-click = "addBook()" >Add book </button>

book_name, book_price  -> books table column names.

$scope.bookName , $scope.bookPrice -> html textbox ngmodel name.

ex:

<input type="text" ng-model="bookName">

<input type="text" ng-model="bookPrice">


Touch with me! Gain more!

Monday, 11 November 2013

                          
 AngularJS - ngAnimate in Filter Array items




In this tutorials, we are discuss about ngAnimate in filter array items. The ngAnimate with the some of the directives,

Directive name Animation type
ngRepeate enter, leave and move
ngView enter and leave
ngInclude enter and leave
ngSwitch enter and leave
ngIfenter and leave
ngShow show
ngHide hide

Filter array items in 3D rotate using ngAnimate.



index.html

<!DOCTYPE html>
<html ng-app>

<head>

<link rel="stylesheet" href="style.css">

<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">

<script src="http://code.angularjs.org/1.1.5/angular.js"></script>

</head>

<body ng-init="products=['iPhone', 'Samsung', 'HTC', 'Nokia' , 'Sony', 
 
'iPhone 5c' 'Apple', 'iPad', 'Lava', 'Surface']; ">

<div class="well">

<form class="form-search">

<input type="text" ng-model="search" placeholder="Filter items..">

<ul>

<li ng-animate=" 'animate' " ng-repeat="product in products |  filter : search">

<a href="#">{{product}}</a>

</li>

</ul>

</form> 

</div> 

</body> 

</html>


Stylesheet file. Support all borwsers.



style.css


.animate-enter,  .animate-leave {

-webkit-transition : 400ms cubic-bezier(0.250, 0.250, 0.750, 0.750) all;

-moz-transition : 400ms cubic-bezier(0.250, 0.250, 0.750, 0.750) all;

-ms-transition : 400ms cubic-bezier(0.250, 0.250, 0.750, 0.750 ) all;

-o-transition : 400ms cubic-bezier(0.250, 0.250, 0.750, 0.750) all;

transition : 400ms cubic-bezier(0.250, 0.250, 0.750, 0.750) all;

position : relative;

display : block;

}


.animate-leave.animate-leave-active,

.animate-enter {

-webkit-transform : rotateX(90deg)  rotateZ(90deg);

-moz-transform : rotateX(90deg)  rotateZ(90deg);

-ms-transform : rotateX(90deg)  rotateZ(90deg);

-o-transform : rotateX(90deg) rotateZ(90deg);

transform : rotateX(90deg)  rotateZ(90deg);

opacity : 0;

height : 0px;

}

.animate-enter .animate-enter-active,

.animate-leave {


-webkit-transform : rotateX(0deg)  rotateZ(0deg);

-moz-transform: rotateX(0deg) rotateZ(0deg);

-ms-transform : rotateX(0deg) rotateZ(0deg);

-o-transform : rotateX(0deg) rotateZ(0deg);

transform: rotateX(0deg) rotateZ(0deg);

opacity : 1;

height :30px;

}



Demo -  Filter Array Items - 3D Rotate




If have any doubts about this tutorial means post comments.

Touch with me! Gain more.





Thursday, 7 November 2013




               How to using ngAnimate in partial template views.

In this tutorial, we are discusses about using ngAnimate in partial template view.

Using nganimate in partial template view.

In this tutorial using two type of modules

1.ngRoute - It is used for routing of templates

2.ngAnimate -  It is used for animation effects in user file.


The ngAnimate is using slides show and moving effects in custom file.

download link - ngAnimate resource file



The ngRoute is using  identify the template location path.

download link - ngRoute resource file



index.html

<!DOCTYPE html> 
 
<!--ng-app module name - animation--> 
<html ng-app="animation">  
 
<!--head tag starts--> 
<head>
 
<!--custom css file--> 
<link rel="stylesheet" href="style.css"> 
 
<!--angularjs resource files--> 
<script src="http://code.angularjs.org/1.2.0rc1/angular.js"></script> 
 
<script src="http://code.angularjs.org/1.2.0rc1/angular-route.min.js"></script>
 
<script src="http://code.angularjs.org/1.2.0rc1/angular-animate.min.js"></script> 
 
<script src="script.js"></script>
 
</head><!--head tag ends--> 
 
<!--body tag starts--> 
<body ng-controller="animateCtrl">
 
<!--click button - animation on or off status--> 
<p>ngAnimate using in templates view : <button ng-click="animateCtrl.switch()">Animation On / Animation Off</button></p><br><br>
 
<!--ng-view - partial template views--> 
<ng-view></ng-view>
 
</body><!--body tag ends-->
 
</html><!--html tag ends-->



Custom script file.

script.js

angular.module('animation', ['ngRoute', 'ngAnimate' ])

.config(function(

$routeProvider  )

{

$routeProvider.

when('/', { templateUrl : 'template-one.html'}). 

when('/alt', {templateUrl : 'template-two.html'});
}).

controller('animateCtrl', function (

$window, $location )

{
this.switch = function()
{
$location.path('/alt' === $location.path() ? '/' : '/alt');

};

}); 
 
 



Custom CSS file.

style.css

ng-view {

display :block;

border : 1px dashed black;

width : 250px;

height : 250px;

position : absolute;

top : 20%;

}

.ng-enter {

-webkit-animation : enter 1s cubic-bezier(.17, .67, .83, .67);

animation : enter 1s cubic-bezier(.17, .67, .83, .67);

}

.ng-leave {

-webkit-animation : enter 1s ease-out reverse;
animation : enter 1s ease-out reverse;

}

@-webkit-keyframes enter {

0% {

background : #f80;

top : 100%;

}

70% {

background : #f08;

}

100% {

background : #8f8;

top : 20%;

}

}

@keyframes enter {


0% {

background : #f80;

top : 100%;

}

70% {

background : #f08;

}

100% {

background : #8f8;

top : 20%;

}

}



template-one.html

AngularJS - ngAnimate



template-two.html

AngularJS - ngRoute



Demo ngAnimate - Plunker


Touch with me! Gain more.

Friday, 25 October 2013


Sorting Array Items using AngularJS.


Part 6:

Array binding tutorial refer : Array binding concept

Sorting is a processes of arranging the items. Ascending or Descending order performing in the array of items. A standard order is often called ascending order [ex :  0 to 9, A to Z], the reverse processes is descending order [ex: Z  to A, 9 to 0].

The order processes in array items using angularjs. It's using ng-repeat module. 
The ng-repeat  is extracting single item from array items and using orderBy reserved word.


Sample Code:



index.html

<!DOCTYPE html>

<!--html tag starts and module - ng-app-->                                                                               
<html ng-app>     

          <!--head tag starts-->   

          <head>           

                    <!--script files-->                                                                                     

                    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs     /angularjs/1.0.8/angular.min.js"></script>

                    <script type="text/javascript" src="controller.js"></script>

                                        
          </head><!--head tag ends-->                                                                                                                   

          <!--body tag tarts-->                                                                                         

          <body>                   

                    <!--controller div starts-->                                                                                                

                    <div ng-controller="Ctrl">

                             <!--sort form starts-->                                                                                       

                             <form name="sortForm">

                                        <!--product table starts-->                                            

                                        <table>                                                                                    
                                                  <thead> 
                                                         <th>Product</th> 
                                                         <th>Price</th>                                            
                                                  </thead>

                                                  <!--ng-repeat and products is an array-->                                                                         <tr ng-repeat="product in products | orderBy : sortingOrder : reverse">                           

                                                        <td>{{product.Product}}</td>          
                                                        <td>{{product.Price}}</td>
                                                  
                                                  </tr>                                   
                                        </table><!--product table ends-->                                               
                              </form><!--sort form ends-->
                                                                                     
                    <button type="btn" ng-click="sortOrder('Product')">Sort Order</button>          </div><!--controller div ends-->
</body><!--body tag ends-->                                                                                         
</html><!--html tag ends--> 


Description :


The ng-app is one of the angular module. It's root of the angularjs models and directives. This module is manage the controllers, services, factory, directives. Single module is contain multiple controllers.

The controller name is "Ctrl". The controller is controlling the data members and member methods. The controller is used for retrive data from database.

The Products is an array. This array is contains product list and prices.  ex: COMPUTER, TABLET, IPHONE, SAMSUNG, NOKIA. In this, accessing array use the ng-repeat model.

Finally, click the button , sortOrder() method goes to controller file.


Controller.js  


//controller statements
function Ctrl($scope)
{

$scope.products = [

                           { Product : 'COMPUTER', Price : '$450'},
                           {Product : 'TABLET', Price : '$150'},
                           {Product : 'IPHONE', Price : '$200'},
                           {Product : 'SAMSUNG', Price : '$150' },
                           {Product : 'NOKIA', Price : '$150'}

                           ]; 

     //function sortOrder definitation

     $scope.sortOrder = function(newSortingOrder)
     {
               $scope.sortingOrder = newSortingOrder;
               $scope.reverse =! $scope.reverse;
     };
};    
                                                                                                

Sample Output:


Ascending Order Products:



Descending Order Products: 




If you have any doubt about this concept means post comments.

Touch with me! Gain more!




Monday, 21 October 2013

Create NGTODO Application using AngularJS


Part 5:

NGTodo is one of the task management application. It's very useful for manage project management or daily activities. Create this application using ng-repeat angularjs modle. The ng-repeat modle is extracting item from array. ng-repeat tutorials array binding tutorials

In this application, three type of methods used. One method is addTask(), it is perform add task in array that is tasks(array). Next method, checkTask() is check box method. It is used for click check box display delete task button. Final method, deleteTask() is deleted task. Note: This tutorials is checked single check box to deleted event is performed and how to check multiple checkbox to delete and inline editing task in next tutorials.

Sample code:




Contoller sample code, ngtodo controller




Sample result:

 Demo this tutorial - Simple NGTodo Application

Tough with me! Gain more!








Saturday, 19 October 2013






Create calculator application use Angularjs


Part 4:

ng-option is one of the angularjs module. ng-option is using to create calculator application. This module is embedded in <select> html tag.

Syntax:

<select                                                                          
             ngModel = "{select ng-model name - string}"    
             [name ="{select name - string}"]                       
             [required]                                                         
             [ngRequired="{string format}"]                         
             [ngOptions = "{comprehension_expression}"]> 
</select>                                                                       


 The angularjs module, ngOptions can be used dynamically create option list in <select> tag. The ngOptions
is consist of array values.

example:

<select                                                                              
           ng-model = "selectedOperator"                                  
           ng-options = "operator for operator in operators" >
</select>                                                                           


 operators is an array, create in controller site,

example:

$scope.operators = [ '+' , '-', '*', '/' ];                                 


sample code:


Demo :

Calculator - jsfiddle

Touch with me! Gain more!








Wednesday, 16 October 2013






How to set limit text in textbox and check email format using angularjs


Part 3:

Normally, no limit to enter number of characters in textbox. Minimum length or maximum length characters set in  textbox so, display Too short! or Too long! messages. This concept is very useful for login page development and more.

Ex:
 Set minimum length characters and maximum length characters in user name or password. User can enter two characters in text box, so display Too short message or user can enter more characters in text box, so display Too length message.

Sample Code:


Angularjs module:

ng-minlength and ng-maxlengt.

Note: external array binding concept include.








Monday, 14 October 2013


AngularJS Array Binding and Filter concept


Part 2:

How to set array binding and filter in angularjs step by step examples in this tutorial.

What is array?

An array stores multiple values in single variable. An array is a special variable, which can hold more than one value at a time.

ex:

In php,

$lang = array("PHP","MYSQL");

access array values use index values,

echo "Front end". $lang[0] . "Back end" . $lang[1].

It is simple array concept in php. How to this type of array concept use in angularjs, it's very simple and easy.

Step 1:

Already, create file structure and include angularjs file.

refer link part 1 tutorial: Create file structure and include angularjs file - part 1.

Follow step by step,


Step 2:

Array initialize use ng-init model name. Array binding in two ways 1.internal array binding, 2.external array binding.

First, explain internal array binding concept. Next tutorials explain external array binding in angularjs.

ex:

ng-init = "employees= [

                             { name:'Ramesh', phone:'9898987678',
                               name:'Vijay', phone:'8976986789',
                               name:'Kumar', phone:'9878987698'
                             }

                             ]"; 


Step 3:

Create text box use input type tag, ng-model name searchText. It's use for enter name or phone search from array and display.


Step 4:

After, create table use html tags. One of the magic code ng-repeat. It's use for extracting single item from array. Its similar
to foreach loop. Filter is reserved word(key word). Filter is filtering the array items. Text box ng-model name assign to filter.

ex:
ng-repeat = "employee in employees | filter:searchText" 


Step 5:

Enter employee name in text box,filter particular employee name and phone number extracting from array.

Result:


enter upper case or lower case letter to search items from array.


refer link: Practical Session! Click Here!


Next tutorial:

How to set limit text in textbox use angularjs?

How to check email format use angularjs?


Touch with me! Gain more!

Monday, 7 October 2013

Angularjs tutorial

Part 1:

simple angularjs.

Angularjs is one of the google product and it is reduce server workload and quickly access data from database.

First of all, How to create simple angularjs.

It is very easy and follow step by step:

Step 1:


First, create one folder, For example, folder name angularjstut.


Step 2:

Create js folder and index.html file in angularjstut folder.


Step 3:


insert angularjs file in js folder.

Download angularjs file use following link,

https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js


file name angular.min.js save in js folder.


Step 4:

Write html codes in index.html


ng-app is angularjs root directory. It is first initialize in top of html tag.

Step 5:


Following, angular.min.js file link in index.html

Magic code in angularjs, data binding in inline code use ng-model.

create text box and assign ng-model name.



Output:


How to set external data binding in html use angularjs?

How to set inline array in html use angularjs?

How to extracting array elements in html use angularjs? ....etc.

this type of concept in another tutorials.

Touch with me! Gain more!.