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

Wednesday, August 23, 2017

AngularJS Expression , AngularJS Controller and Bootstrap example

Let's take a look example of area calculation of different shapes using AngularJS Expression and Controller.
  • Square and Rectangle shape's Area calculation using AngularJS expression
  • Circle and Triangle shape's Area calculation using AngularJS controller.
Mentioned page is also design using bootstrap to give nice look and feel.
Please check my previous blog post related to following topics as mentioned.
Please check LIVE demo of given example here : Click Here


AngularJS Expression
AngularJS binds data to HTML using Expressions. It can be written inside double braces as
{{ expression }}. It can also written inside ng-bind directive. In example you will see Square and Rectangle area calculation expression inside AngularJS Expression. AngularJS will resolve the expression and return the value to the HTML element.

Square 
square variable which is width/height of shape and we are initializing variable using ng-init directive.
The same variable is bind with input textbox using ng-model directive.
The Square shape area calculation equation added inside AngularJS Expression as{{sqaure*sqaure}}

Rectangle 
Rectangle shape's width  rectwidth and height rectheight initialize using ng-init directive. Both variables are bind with input textbox using ng-model directive. The Rectangle Area calculation equation is added inside AngularJS Expression as {{rectwidth*rectheight}}

AngularJS Controller
AngularJS applications are controlled by controller. The ng-controller directive is used to define a controller object. AngularJS Controller is javascript object created using JavaScript Object Constructor notation.

In my application ng-app="myApp" directive which is defined AngularJS application. This application run inside define <div> element.The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller. When Controller object initialized from JavaScript language it also passes $scope object. Within $scope object we are initializing variables for Circle and Triangle which also contain methods to perform Area calculations.

Circle
Circle's radius variable or attribute is initialized inside controller object in javascript line  $scope.radius=6;  The variable is also bind with input textbox using ng-model directive. To perform circle area calculation we need MATH.PI variable so we are passing $window service also using AngularJS Service Injection feature. $scope.circleArea = function do the calculation and which is added in HTML using AngularJS Expression as {{circleArea()}}

 Triagle
Triangle's shape base and height variable or attributes are initialized inside controller object in javascript line $scope.base=4;    $scope.height=7;  Both variables are bind with input textboxes using
ng-model directive. $scope.triangleArea = function do the calculation and which is added in HTML using AngularJS Expression as {{triangleArea()}}

AngularExpression.html

Hope this example give you some basic idea about AngularJS Expression & Controller.

Please post your questions or comments bellow. Thank You !

Sunday, August 13, 2017

AngularJS + Bootstrap Example ! step by step webpage coding.

In my previous post we have seen how to setup AngularJS & Bootstrap in your development machine.
If you are not clear with introduction of AngularJS & Bootstrap then first please review my following posts.

In this post we see one simple AngularJS + Bootstrap small example and it's page development step by step. Please check running page demo here : Click Here


We never get final expected webpage in straight one writing so i always go step by step approach of any webpage or application development.

Step 1:  First step is to create webpage with basic bootstrap template just to see our bootstrap require references are added proper or not.
<html>
  <head>
    <title>Bootstrap 101 Template</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    </head>
  <body>
    <h1>Hello, world!</h1>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://code.jquery.com/jquery.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

Step 2 :  Once you check webpage change webpage title and webpage header"AngularJS & Bootstrap Example"

<title>AngularJS & Bootstrap Example</title>
<body>
<h2>AngularJS + Bootstrap Example</h2>

Step 3 : Go to the Bootstrap documentation page and find the component which you want to use in your page. I am going to use Panel for my "Order Summary" page so i will go to following link and get the sample of  Panel. https://getbootstrap.com/docs/3.3/components/#panels

<div class="panel panel-default">
  <div class="panel-body">
    Panel content
  </div>
  <div class="panel-footer">Panel footer</div>
</div>


Step 4 :  Change panel "Header" , "Content" & "Footer" as per following code sample.You will see that i have used Bootstrap CSS in most of all HTML elements like Input,Label,Panel which give nice view on webpage.

Step 5 :  After you test your Page layout now it's time to add AngularJS Directives as per following
  • Add reference of AngularJS library in your page
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  • Add ng-app directive which  define root of Angular App
  • Add ng-model="price1" , ng-model="price2",ng-model="price3"  which bind HTML element with data value.
  • Add Total as Angular Expression like "{{ price1 + price2 + price3 }}"
AngularExample2.html

Please post your comments or questions. Thank you !

Friday, August 11, 2017

What Is AngularJS? Introduction and Basic Example.


AngularJS is a structural framework for dynamic web apps. AngularJS extends HTML with new attributes.

AngularJS is a JavaScript framework. It can be added to an HTML page with a <script> tag.

AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions

AngularJS is perfect for Single Page Applications (SPAs).

It is a library written in JavaScript and can be added to a web page with a script tag:


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

Let's take a look basic example so you will come to know how AngularJS directives or attributes which are adding with HTML.

AngularJS Example : 

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
   <div ng-app="">  
            <h3>Name: <input type="text" ng-model="name"></h3>  
            <h2 ng-bind="name"></h2>
     </div>
</body>
</html>

Check example output here  : Test Page Here 

 AngularJS starts automatically when the web page has loaded.

The ng-app directive tells AngularJS that the <div> element is the "owner" of an AngularJS application.

The ng-model directive binds the value of the input field to the application variable name.

The ng-bind directive binds the innerHTML of the <h2> element to the application variable name.

SPA : Angular is very famous for Single Page Application due to this binding benefit and with AJAX call we can bring data from server using web api or other call mechanism and those modules attributes updates in javascript file and changes to UI automatically reflects.

We will learn more for AngularJS in my next post. Thank You !