Getting Started - Create a simple Single Page Application (SPA) - Angularjs


Create a Single-Page Application (SPA) - Angularjs

 - This article is Part of a series :
  1. Getting Started - Create a simple Single Page Application (SPA) - Angularjs
  2. Getting Started - Entity Framework Code First
  3. Getting Started - Web API
  4. Getting Started - Create a SPA And use AngularJS $http Service



A single-page application (SPA) is a web application or web site that fits on a single web page with the goal of providing a user experience similar to that of a desktop application. (wikipedia).

- Create new html page named main or index 
  • use  ng-view  In order to determine loading part (html template) , your main page will look like this :



//main.html


<!DOCTYPE html>
<html ng-app="myLibrary">
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="../Content/bootstrap.css" rel="stylesheet" />
    <script src="/Scripts/angular.js"></script>
    <script src="Scripts/angular-route.js"></script>

</head>
<body>
    
    <div style="margin-top:5em" class="container">
        <!-- your View will appear here  -->
        <div ng-view></div>
    </div>

</body>

</html>




-  add new folder named templates 

- add 2 new pages to templates folder named books.html & authors.html


  • The books.html & authors.html are samples pages contain table of authors and table of books (we'll go through implementation later in this article)
- now we will configure our angularjs app to use routing 

  • Add 3 JavaScript files in new folder (or script folder) named  (myLibrary.js , booksController.js , authorsController.js ) . 
  • Open myAppModule.js and write the following configuration :





//myLibrary.js


var myLibrary = angular.module("myLibrary", ['ngRoute'])
    .config(function ($routeProvider) {

       
        $routeProvider.when('/books', { templateUrl: '/templates/books.html', controller: 'booksController' });
        $routeProvider.when('/authors', { templateUrl: '/templates/authors.html', controller: 'authorsController' });

        //default
        $routeProvider.otherwise({ templateUrl: '/templates/books.html', controller: 'booksController' });


    });
  • Open booksController.js and create books array for $scope.books : 

//booksController.js


myLibrary.controller("booksController", function ($scope) {
    
    $scope.books = [
        { name: 'A brief history of time', author: 'Stephen Hawking' },
        { name: 'Death: The Final Stage of Growth', author: 'Elisabeth Kübler-Ross' },
        { name: '50 Mathematical Ideas You Really Need to Know', author: 'Tony Crilly' },
        { name: 'The Grand Design', author: 'Tony Crilly', author: 'Stephen Hawking'}
    ];
 
});
  • Same steps in authorsController.js : 
//authorsController.js


myLibrary.controller("authorsController", function ($scope) {

           $scope.authors = [
           { authorname: 'Stephen Hawking' , numOfBooks : 2 },
           { authorname: 'Elisabeth Kübler-Ross', numOfBooks: 1 },
           { authorname: 'Tony Crilly', numOfBooks: 1 }]
       });




- Open books.html and create books table (I used bootstrap for styling) :








//LibraryDbContext.cs


<div>
    <table class="table table-condensed table-hover">
        <thead>
            <tr>
                <th>
                    Book Name

                </th>
                <th>
                    Author
                </th>

            </tr>
        </thead>
        <tbody  ng-repeat="book in books">
            <tr>
                <td>
                    {{book.name}}
                </td>
              
                <td>
                    {{book.author}}
                </td>
            </tr>
         
        </tbody>
      
    </table>

</div>

  • To do authors.html(same steps).

- add scripts and links references to main.html :

//main.html





<!DOCTYPE html>
<html ng-app="myLibrary">
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="../Content/bootstrap.css" rel="stylesheet" />
    <script src="/Scripts/angular.js"></script>
    <script src="Scripts/angular-route.js"></script>
    <script src="Scripts/myApp/myLibrary.js"></script>
    <script src="/Scripts/myApp/booksController.js"></script>
    <script src="/Scripts/myApp/authorsController.js"></script>


</head>
<body>
   
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Library</a>

            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a href="#books">Books</a></li>
                    <li><a href="#authors">Author</a></li>

                </ul>
              
            </div>
        </div>
    </div>
    <div style="margin-top:5em" class="container">
        <div ng-view></div>
    </div>

</body>


</html>

main page








- That is it , Click here to get a copy from github.

Your First Angular App

It's easy to use Angular , I think it's more easy and fun than jquery , so let's see a basic example .


  • To create basic Angular app you have to create a module then attached Controller to it, in your script you can define your module name and your  controller like this : 
//javascript.js file


angular.module('myApp', []).controller('myController', function ($scope) {
    //write whatever code and functions herer


});

  • In your html page or in your main page refer to your module in <html> attrubite or <body>  and  refer to your controller in the section that you need to integrate with that controller like this :
//index.html


<html ng-app="myApp">
<head>
</head>
<body ng-controller="myController">
  
</body>
</html>
  • back to your script , use $scope as var , it may hold array or function  :

//javascript.js file


angular.module('myApp', []).controller('myController', function ($scope) {

    $scope.numbers = [1, 2, 3, 4];

    $scope.alertnum = function (num) {
        alert('you have clicked : ' + num)
    };


});

  • Use ng-repeat to loop through $scope.numbers in Dom and ng-click to invoke $scope.alertnum  like this :

//index.html

<html ng-app="myApp">
<head>
</head>
<body ng-controller="myController">
    <div ng-repeat="num in numbers">
        <a ng-click="alertnum(num)" href="#">
            {{num}}
        </a>

    </div>
</body>

</html>

  • here is a working example

See the Pen Your First Angular App by Mohamed Salah (@saad306) on CodePen.



مثال بسيط علي انشاء WCF Service الجزء الثالث


·         انشاء WCF Client Application  

-          من Solution Explorer  قم باضافة Project جديد من النوع Console Application  باسم BirthdayClient

-          من Solution Explorer  قم باضافة مرجع Add Reference  الي System.ServiceModel assembly  الي BirthdayClient Project بالضغط بالزر الايمن للماوس عليه ثم اختيار Add Reference

-          ايضا قم باضافة  Service Reference  بالضغط بالزر الايمن للماوس علي BirthdayClient  ثم اختار Add Service Reference  ومن الصفحة التي ستظهر قم بالضغط علي Discover  ليقوم بالبحث عن الـ Service  المتوفرة لتظهر لك Service  التي انتهينا من انشائها قم بالضغط عليها وفي اسف الصفحة في مريع النص Name Space  قم بتغير الاسم الي BirthdayService ثم قم بالضغط  علي Ok كما بالشكل


-          بالتوجه App.config  في BirthdayClient   وفتحه ليظهر الكود سوف تجد ان WCF Service  قامت بعمل تهيئة الاتصال بالـ Service  كما بالكود التالي

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IBirthdayService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:12516/Service.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IBirthdayService" contract="BirthdayService.IBirthdayService"
                name="BasicHttpBinding_IBirthdayService" />
        </client>
    </system.serviceModel>
</configuration>

-          ستجد في الجزء الخاص بالـ <client> تحديد طريقة الاتصال بـ Service  في <endpoint> يوجد معلومات الاتصال وهي تتكوت من 3 اجزاء address , binding , contract

-          الان قم بفتح Program.cs  ثم قم باضافة المراجع الاتية

using System.ServiceModel;
using BirthdayClient.BirthdayService;

-          في Main method  قم بانشاء New Instance  من BirthdayService  ليصبح الكود كالاتي

static void Main(string[] args)
        {
            BirthdayServiceClient proxy = new BirthdayServiceClient();

        }
-          الان يمكنك استخدام Service Method  التي انشائنها من قبل والاتي هو مثال استخدام  ListFirstName() Method

static void Main(string[] args)
        {
            BirthdayServiceClient proxy = new BirthdayServiceClient();

            Console.WriteLine("Now The Console will Write All First Name");
            string[] info = proxy.ListFirstName();

            foreach (string name in info)
            {
                Console.WriteLine(name);
            }

            proxy.Close();
            Console.ReadKey();

        }

-          قبل ان تقوم باختبار الـ Client Project   يجب عليك وضع الـ Startup Projects  بالضغط علي الـ Sultion (BirthdayService) بالزر الايمن للماوس واختيار Set Startup Projects

-          من الصفحة التي ستظهر قم باختيار Multiple startup projects وقم بتحديد الـ Projacts  الاتية باختيار Start كما بالشكل


-          بعد التحديد والضغط علي Ok  قم بالضغط علي Ctrl + F5  للاختبار كما بالشكل



-          الي هنا نكون قد انتهينا من هذا الجزء في الجزء القادم سوق اقوم بشرح كيف تقوم بعمل deploying  للـ Service  علي IIS   (Internet Information Services)