Saturday, August 9, 2014

Control Your App’s Behavior (Controller)

Chapter – IV
Control Your App’s Behavior (Controller)

In our previous Hello World example we simply defined a model [i.e. data.message] in our ng-model directive then we set some value on it and at the same time get the value from it. But how about we pass a predefined message to show at start up. Let me clarify what I’m saying. Let’s modify our Hello World Program to look like this,

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>Angular Controller</title>
</head>
<body>
    <div ng-controller="messageController">
        <input type="text" ng-model="data.message" />
        <h1>{{data.message}}</h1>
    </div>

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

    <script>
        function messageController($scope) {
            $scope.data = {
                message: "Fiyaz Hasan"
            };
        };
    </script>
</body>
</html>

OUTPUT:



All we've done here is shifted the input the h1 tag in a new div tag. Then we've inserted a new Angular directive called ng-controller in that div. Angular controllers are just functions with some extra ordinary capabilities. From a simplest viewpoint, a controller in a MVC application routes an incoming request [e.g. an URL typed in the browsers address bar] to a specific business logic section where the request is parsed and a response is sent back after some logic manipulations.

So, with our ng-controller set to the string messageController, we are saying that in our script we've a function called messageController which is responsible for setting the message property of the data object. As you can see I’ve set the message property value to my name.

Now you can ask that how am I getting the data object’s message property value without returning the object from the function? Look at the messageController function again. As you can see I’m passing a variable named $scope to our messageController function. Well it’s not like I've came up with a variable name like $scope to pass to my controller function, actually it’s an Angular defined variable or we can say a provider [We will talk about providers later]. With this $scope provider we are just sending the scope of a controller to be active. Which means that only the div containing ng-controller=”messageController” will flow the data object’s property values of messageController function. It’s more like we are defining a specific functionality for a specific area within our document. Likewise we are able to define controllers as much as we need for our apps to serve different functionalities for different sections in the document. For example let's create another controller which will do some math on a number passed from our UI. The final code look like this,

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>Angular Controller</title>
</head>
<body>
    <div ng-controller="messageController">
        <input type="text" ng-model="data.message" />
        <h1>{{data.message}}</h1>
    </div>

    <div ng-controller="mathController">
        <input type="text" ng-model="data.number" />
        <h1>{{data.squareRoot()}}</h1>
    </div>

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

    <script>
        function messageController($scope) {
            $scope.data = {
                message: "Fiyaz Hasan"
            };
        };
        function mathController($scope) {
            $scope.data = {
                number: 9,
                squareRoot: function () {
                    return Math.sqrt(this.number);
                }
            };
        };
    </script>
</body>
</html>

If you run your app you will see something like this in you browsing window,

OUTPUT:



So, in our new controller instead of getting a static property value, we perform a square root function on the number passed from the UI. Look carefully at

<h1>{{data.squareRoot()}}</h1>

As you can see you are executing a method in the data object called squareRoot. Syntax written in {{ }} is called expression. You can execute any kind of property value or functions in the {{ expression }}. You can also add some filtering and sorting expressions which we will see later.

I guess from now you know how to work with Angular controller. I will see you in the next post.


Sunday, August 3, 2014

Hello from Angular (Data Binding)

Chapter – III
Hello from Angular (Data Binding)

[If you are finding it difficult to grasp on what I wrote so far, don’t worry! Stick with me. I will take baby steps to make you familiar with Angular.js and after seeing some simple examples, you will understand what I’m trying to achieve here with Angular.js]

So let’s see what we've got as our first example. If I’m right, when I said our first example. You are thinking like, “No!!! Not the hello world again!!!!” Actually you are right it’s our very old friend Hello World Program. But this time it’s not about printing the string “Hello World” on a button click rather we’ll bind a DOM element [e.g. <h1></h1>] to a input element [e.g. <input type=”text”/>] and update the text in h1 tag according to the text typed in the input field. And all of this will be happening in real time. It’s a very simple example of Angular.js Data Binding. So before we dive into some coding let’s have the very basic starter template for our Angular.js Hello World App. For that, first we will have to download the Angular.js library from https://angularjs.org/. If you already clicked the big download button there then you are seeing a floating panel like this,



At the time of writing this post the 1.3.x (latest) was in beta version. So we will work with 1.2.3 (legacy) version. As you can see we also have a CDN (Content Delivery network) link. I’m going to use the CDN link because I’ve got a stable internet connection. So our basic template will look like this,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Angular Starter Template</title>
</head>
<body>

    <input type="text" />
    <h1></h1>

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

</body>
</html>

Okay as you can see its all HTML up there. Next we will have to tell this HTML file to behave like an Angular.js app. This is the most important part of all since it instantiate the Angular.js library into your HTML file. To do that we’ll have to use an Angular Directive called ng-app in the opening <html> tag like this,

<html lang="en" ng-app>

So, you have an Angular.js app. Next you will add another directive called ng-model to you app and this time it is going into the <input type=”text”/> tag. It should look like this,

<input type="text" ng-model="data.message"/>

Doing so, we are requesting our dear friend Angular to create a POJO (Plain Old JavaScript Object) named data with a property named message behind the curtains.

Last of all we will have to bind the <h1></h1> to the model we’ve just created. To do so write the following into the h1 tag

<h1>{{data.message}}</h1>

With this syntax we are telling Angular to attach the message property of our data object to the <h1></h1>. Since we are having a two way binding here, so when we type something in the text box, it sets the message property to our typed string and at the same time it gets the message property value and shows it in the h1 tag. 

And we are finished. Browse your HTML file with your favorite browser. Type something in the text box and see what happens. Awesome!!! Right??? I’m pasting my output here. Mine is something like this,


I hope you get it, see you in the next post.






Saturday, July 19, 2014

3D’s of Angular.js

Chapter – II
3D’s of Angular.js

Data Binding


One of the core feature you will want in every presentation or client side development framework is data binding. Unlike any other frameworks angular provides two way data binding. In angular all you will have to do is to connect the view to a model. And you are done! Later in your application if the model changes the view get changed and if the view changes the model get changed. This feature was so much needed because before that developers have to write additional codes to update the views and models separately. Now there is less code and less code is always good!

Directives


Directives are like your custom HTML. Angular has many built in directives that makes your HTML declarative. Some of them are ng-view, ng-model, ng-controller etc. You can build your own directives and to make a custom HTML element, attribute or class. By default all directives that you create are attributes if you don’t define its mode. We will take a deeper dive into directives later.

Dependency Injection


Dependency injection is how your angular modules talk to different services throughout the applications [We will talk about modules and services later]. As I already said you can use other library like jquery with angular. To do that you will have to inject the library dependency where you will be needed it. Angular’s own services are separated into different files. To use these different services throughout different modules, you will have to inject the dependencies. For example if you want to use angular’s animation service you will have to inject the ngAnimate into your application module.