неделя, 23 юни 2013 г.

AngularJS Step By Step Tutorials

I have been developing with AngularJS lately and I am starting step by step tutorials in my blog.

Here is the first article from the series:

Hello AngularJS

AngularJS is a client-side JavaScript framework by Google. It helps you to write organized and testable code on the client side. AngularJS shines particularly in the so called single page applications (SPA). If you are curious why it is called AngularJS - it is because the expressions in AngularJS are surrounded by brackets - {{ expressionHere }}

All you need to start using AngularJS is to include the library .js file in your html page.

<html ng-app>
    <head>
        <title>AngularJS is cool</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js">
        </script>
    <head>
    <body>
        {{'Hello AngularJS'}}
        <br/>
        {{ 7 + 9 }}
    </body>
</html>

As you may have guessed this example prints Hello AngularJS and 16 in your page.

You learned three important things about AngularJS from this simple example:
  1. You must include the AngularJS library through the script tag.
  2. Expressions in AngularJS are surrounded by {{ }}
  3. You must use the ng-app directive to bootstrap your application. A directive in AngularJS is a custom component - extension to the standard HTML. ng-app is a built-in directive in AngularJS. You can create your own custom components, i.e. directives. But this will be a topic of another tutorial.

Simple Calculator

Now let's create something more useful - a simple calculator. You enter two numbers and an operator and our program will calculate the result.

Your business logic in AngularJS is done in the so called controllers. You declare variables and methods in controllers. Controllers are created in separate js files which you include in the page. From the html view the controller is used with the help of the ng-controller built-in directive.

<html ng-app>
    <head>
        <title>AngularJS Calculator</title>
        
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js">
        </script>
        
        <script src="calc-controller.js"></script>
    <head>
    <body>
        <div ng-controller="Calculator">
            <label>A:</label>
            <input ng-model="firstNumber" type="text"> <br/>
            
            <label>Operator:</label>
            <select ng-model="selectedOperator" 
                    ng-options="operator for operator in operators"></select> <br/>
            
            <label>B:</label>
            <input ng-model="secondNumber" type="text"> <br/>
            
            <button ng-click="calculate()">Calculate</button> <br/>
            
            Result: {{result}}
        </div>
    </body>
</html>

Here is our controller declared in the calc-controller.js:
function Calculator($scope) {    
    $scope.operators = ['+', '-', '*', '/'];
    $scope.selectedOperator = $scope.operators[0];
    
    $scope.calculate = function() {
        var A = parseInt($scope.firstNumber);
        var B = parseInt($scope.secondNumber);
        var C = 0;
    
        switch ($scope.selectedOperator) {
            case '+':
                C = A + B;
                break;
            case '-':
                C = A - B;
                break;
            case '*':
                C = A * B;
                break;
            case '/':
                C = A / B;
                break;                
        }
        
        $scope.result = C;
    };
}

We specified the controller name using the ng-controller directive:
 <div ng-controller="Calculator">

The controller name must match the name of the function - function Calculator($scope)

For each controller a local $scope is created. You can access the information in the page and also modify it through the $scope. You can also declare methods in the scope. All things you declare in the $scope are accessible only in the part of the html page where the ng-controller directive is specified.

Using the ng-model directive you bind the input information (the numbers and the operator) to the $scope. Then the controller can access the input information through the $scope parameter variable.

Using the ng-options directive we populate the select combobox with data from the controller.

Using the ng-click directive, the calculate() method in the controller is called, when the button is clicked:
<button ng-click="calculate()">Calculate</button>

Now look really carefully at this line in the controller:
$scope.result = C;

After it is executed, AngularJS will automatically update the {{result}} expression in the UI. This automatical bidirectional binding between the UI and the scope is really powerful and makes your life easier.

I hope this introductory article was useful to you. You learned that controllers and directives are a key part from AngularJS and created a simple calculator.

In the next article we will tackle the next important part - the AngularJS filters, which are used to format your data in the UI.


неделя, 19 май 2013 г.

Sorry for skipping one week!

The solution for the previous puzzle is Qf4!
When black respond exf4 white mate with Bg4!, otherwise white mate with Qf7!

Here is the puzzle for this week:

3n4/P1R5/6kp/7p/7R/8/6K1/1n2BQ1B w - - 0 1

White to move, mate in two

Happy solving!

неделя, 5 май 2013 г.

The solution for the puzzle from the previous week is Qe8!
When black respond Rxe8 white mate with Nf5!, otherwise white mate with Qd7!

Here is the puzzle for this week:

8/R5p1/3pk3/3pp3/Q7/8/1B3K1R/3B4 w - - 0 1

White to move, mate in two

Happy solving!

неделя, 28 април 2013 г.

I skipped one week, because I was very busy. Neverthless here is the new puzzle.

The solution for the puzzle from the previous week is Qc3!
When black respond dxc3 white mate with Ld6!, otherwise white mate with Qd4!

Here is the puzzle for this week:

2B2rb1/P7/Nppk2n1/3p4/3B4/6NP/2R1Q1PK/3R4 w - - 0 1

White to move, mate in two

Happy solving!

неделя, 14 април 2013 г.

The solution for the puzzle from the previous week is Re3!
When black respond fxe3 white mate with Qg4, otherwise white mate with Re5!

Here is the puzzle for this week:

8/2P2P2/p3pb2/P3kp2/3p4/BP6/2Q2N1P/3R3K w - - 0 1

White to move, mate in two

Happy solving!

неделя, 7 април 2013 г.

The solution for the puzzle from the previous week is Rf3!
When black respond d4, white mate with Rxf4!

Here is the puzzle for this week:

1Bbb2N1/8/6P1/2P2k2/3p1p2/1R6/6R1/3Q2K1 w - - 0 1

White to move, mate in two

Happy solving!

неделя, 31 март 2013 г.

The solution for the puzzle from the previous week is Qxa8!
When black respond Rxa8, white mate with Rxh4otherwise they mate with Qa4!

Here is the puzzle for this week:

5N2/2p5/2k5/P2p3P/5p2/Q1P4R/6B1/1R4K1 w - - 0 1

White to move, mate in two

Happy solving!