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

Wednesday, July 13, 2016

ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json

In this blog post we are going to add the jQuery, AngularJS, and bootstrap libraries to our ASP.NET Core application.  Normally we will use NuGet to bring in these libraries but ASP.NET Core gives you the option to use bower to configure the dependencies that you will need on the client-side.

Here are the steps to import the client-side dependencies into our project:

1. First let's make bower.json part of the "NorthwindCafe.Web" project, by right click on the bower.json file, and then choose "Show in Solution Explorer"

2.  Open the bower.json file the markup should look like this

{
"name": "asp.net",
"private": true,
"dependencies": {
}
}

3. Change the markup to look like the following
{
"name": "asp.net",
"private": true,
"dependencies": {
"bootstrap": "~3.3.6",
"angular": "~1.5.7"
}
}

4. Save the bower.json file, once the file is saved a folder called "lib" will be created and will contain the bootstrap and AngularJS libraries




ASP.NET Core Posts:
  1. How To Create An ASP.NET Core Application From Scratch
  2. ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
  3. Enable ASP.NET Core to Serve Static Files
  4. Enable MVC On ASP.NET Core Application


Sunday, June 26, 2016

AngularJS SPA Pt 3 : Refactor Code to Not Use Global Variables (Shopping List App)

In the previous blog we got Angular-Seed to work with a module and a controller.  However, we put everything in the global scope.  The problem with that is that there are many JavaScript libraries that might be using the same variables as we are, or if we are working with other developers.  The way we can mitigate this problem is to wrap our modules and controllers in an anonymous function.  Think of an anonymous function as a wrapper or a container to hold our modules and controllers.  Another term developers like to refer to anonymous function is an IIFE.  Whatever it's called it's always good practice to avoid putting things in the global environment if it can be avoided.

Here are the steps to take the modules and controllers out of the global environment:

1.  First let's wrap the module in an anonymous function.  The source code for the app.js file should look like the following


(function(){
'use strict';

var app = angular.module('shoppingList',['shoppingController']);

}());


The code above wraps the application in anonymous function and assigned to the variable call "app"

2.  Now open the "shoppingController.js" in the "controllers" folder and wrap the "shoppingController" in anonymous function, the source code for the file should look like the following


(function(){
'use strict';

angular.module('shoppingController',[])
.controller('shoppingController',["$scope",function($scope){
$scope.shoppingListName="My Shopping List";
}]);

})());


3.  Open up the "index.html" file and make sure the source code looks like the following.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Shopping List App</title>
<link rel="stylesheet" type="text/css" href="app.css">
</head>
<body ng-app="shoppingList" ng-controller="shoppingController">
{{shoppingListName}}
<script type="text/javascript" src="js/lib/angular/angular.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/controllers/shoppingController.js"></script>
</body>
</html>

4. In the command line where the root folder is type in "npm start"

5.  Type int the following URL in your browser http://localhost:8000/ and you will see the following



Note: In order to see the application in your browser you must first run the "npm start" in your angular-seed folder first

In this tutorial we've taken some steps to future proof our application so that we are not working in the global environment

Posts In The AngularJS SPA Application Series:

Saturday, June 25, 2016

AngularJS SPA Pt 2 : Preparing Angular-Seed For The Shopping List Application

The previous blog we setup the Angular-Seed boilerplate template for our SPA application.  In this blog we are going to prepare the Angular-Seed template for our SPA application which is going to be a simple shopping list application.  The steps below describes the steps to clean up some of the pages in the angular-seed template for our application.

1.  Delete the content of the "index.html" page in the angular-seed main folder.   It is located in the main folder of the angular-seed template "/angular-seed



2.  The source code file "index.html" should look like the following

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Shopping List App</title>
<link rel="stylesheet" type="text/css" href="app.css">
</head>
<body>
<script type="text/javascript" src="js/lib/angular.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/controllers/shoppingController.js"></script>
</body>
</html>

The code above provides the plumbing for our SPA application.  It's written using the HTML5 template format so that it is HTML5 compliant.  The app.css contains all the styles associated with the application, the "angular.js" file contains the angularJS library framework.  The "app.js" file contains the JavaScript codes that will be used for this application.  The "controller.js" file contains the controller for the application.

AngularJS uses the MVC framework, Model-View-Controller.  Basically they all work together to make the application work.  In a simple explanation the model is the data, the view is what the users see, and the controller is the flow of the application.  The next step is the define our application in the app.js file.

3.  Move the file the "app.js" into the "js" folder then open the file in your favorite text editor




4.  The source code for the app.js file should look like the following

'use strict';

angular.module('shoppingList',['shoppingController']);


The code above defines the application name 'shoppingList', and the modules that the application is dependent on ['shoppingController'] in this case the application is dependent on the shoppingController module.  In the next step we will create the controller for the shoppingList application

5.  Add a folder in the "js" folder and call it "controllers" then create a file call "shoppingController.js" in the "controllers" folder and source code for the file should look like the following


'use strict';

angular.module('shoppingController',[])
.controller('shoppingController',["$scope",function($scope){
$scope.shoppingListName="My Shopping List";
}]);


The code above registers a new module called shoppingList.controller with no dependencies.  The controller itself passes in the angular object $scope.  The $scope object in angular act as a conduit between the view and the controller.  Meaning anything that is defined in the $scope object is accessible in the view (.html) page.  In the example above we defined a variable in the $scope object call "shoppingListName".  Since it's defined in the controller we can now access the data from our view (index.html) page.  Now we are ready to make some changes to our "index.html" page to make this an angularJS SPA application.

6.  Open up the "index.html" file and make sure the source code looks like the following.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Shopping List App</title>
<link rel="stylesheet" type="text/css" href="app.css">
</head>
<body ng-app="shoppingList" ng-controller="shoppingController">
{{shoppingListName}}
<script type="text/javascript" src="js/lib/angular/angular.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/controllers/shoppingController.js"></script>
</body>
</html>

7. In the command line where the root folder is type in "npm start"

8.  Type int the following URL in your browser http://localhost:8000/ and you will see the following



Note: In order to see the application in your browser you must first run the "npm start" in your angular-seed folder first

In this tutorial we've taken care of the plumbing in an AngularJS application.  We gave created a module, associate a controller, define a variable in the controller's scope and then display the scope variable in a view.  We basically have all of the AngularJS app compnents working.

    Use Bower to Get Client Dependencies

    Bower is a JavaScript tool to get client dependencies in your project using npm packages.

    The requirement for bower is that you need to install Node.js first.  You can follow along in the this blog to install node.js, After node.js is installed open the command line and type the following command

    npm install -g bower


    The command above will install bower in your system globally.  Now we can use bower install packages individually, but the convenience of bower is in the bower.json file.  With the bower.json file we can specify all the dependencies that our project will need in one configuration file.

    Here are the steps:

    1.  Create a folder call "AngularShoppingApp"

    2.  Create a file call bower.json in your favorite text editor

    3.  The content of the bower.json file should look like this

    {
    "name": "ShoppingApp",
    "private": true,
    "dependencies": {
    "angularjs": "~1.5.7",
    "bootstrap": "~3.3.6"
    }
    }

    The important thing to look at is the dependencies section.  As you can see we listed out our dependencies in the json file and let bower take care of it for ourselves.
    The ~ symbol tells bower to get any version that starts with the numbers after it.  The other notable values are the "name" which is the name of the application.

    4.  Now we want to specify the location of where we want the bower components are installed in our application.  For that we want to create the .bowerrc file .  the only variable we want to set in the file "directory" file, which specifies where we want the bower components to be install.  Let's install the bower components in the "js/lib" folder. So in the .bowerrc file type in the following

    {
    "directory": "js/lib"
    }

    4.  Now open up the cmd file and navigate to the folder where the bower.json file is located, and type in the following command

    bower install

    after you type bower install the JavaScript libraries will be installed in the "js/lib" library


    The folder structure of your app should look like the following:


    Wednesday, July 1, 2015

    AngularJS SPA Pt 1 : Setting Up Angular-Seed

    angular-seed is an AngularJS application skeleton template.  An application skeleton let's gives you a boiler plate template so that you don't have to start the application from scratch.  You can get the source code from https://github.com/angular/angular-seed.  Below are the steps to use angular-seed ass your application template for AngularJS.

    Step-By-Step Instructions:

    1.  Go the page https://github.com/angular/angular-seed

    2. Open Git GUI.  if you don't have Git install you can follow this blog.

    3.  In the Github angular-seed page copy the "HTTPS clone URL"

    4.  In Git GUI click on the link "Clone Existing Repository"


    5.  In "Source Location" paste the angular-seed clone URL that you've just copied, then on the "Target Directory" field type in or browse to the folder that you want to store the repository in on your local machine.  Then click "Clone"


    6.  Click on "Expore Working Copy" to see the repository on your local machine


    7.  As you can see the repository has been cloned in your local PC


    8.  Now we want to add npm NodeJs to the node package manager to run, debug, and package the AngularJS, follow this blog to setup NodeJs

    9.  In the command line navigate to the angular-seed git in your local machine, then type "npm install" without the double quotes.  This process might take awhile, so don't cancel the process especially if it's the first time you run the command.

    10. Now open up the bower.json file then add the bootstrap library to the list of dependencies, by typing in "bootstrap": "~3.3.6" the final markup should look like the following

    {
    "name": "angular-seed",
    "description": "A starter project for AngularJS",
    "version": "0.0.0",
    "homepage": "https://github.com/angular/angular-seed",
    "license": "MIT",
    "private": true,
    "dependencies": {
    "angular": "~1.5.7",
    "angular-route": "~1.5.7",
    "angular-loader": "~1.5.7",
    "angular-mocks": "~1.5.7",
    "html5-boilerplate": "^5.3.0",
    "bootstrap": "~3.3.6"
    }
    }


    11.  Open the .bowerrc file and change the directory to "app/js/lib", the final markup should look like the following

    {
    "directory": "app/js/lib"
    }

    Note: If you need more information on bower you can visit this blog that I wrote about it

    12.  After "npm install" command finish executing type in "npm start" in the command line to have nodejs start a local web server



    12. Open up the index.html file in the app folder and replace all the instances of bower_components/ path with js/lib/



    13.  In the browser type in the http://localhost:8000, you will see that angular-seed is hosted on the web server