First, let’s create content that is need for our application by creating the HTML framework and then adding the static view components.
Let’s first create a blank framework and tweak it for mobile devices if necessary.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>e-Shopping List</title>
</head>
<body>
</body>
</html>
Now that we have a framework established, let’s add static view components to it.
<body> <div>NOTE: This is standard HTML code with two placeholdes (itemand erroMessage) . There is nothing dynamic here.
<h1>e-Shopping List</h1>
<input placeholder="Add shopping items here">
<button>Add</button>
<p>Press <span>X</span> to delete a shopping item.</p>
<ul>
<li><span>X </span>{{item}}</li>
</ul>
<p>{{errorMessage}}</p>
</div> </body>
<title>e-Shopping List</title> <style>
.panel { font-family:Arial;
width:250px;
background-color:lightgray;
margin:10px;
padding:10px;
box-shadow:5px 5px 5px gray;
-webkit-box-shadow:5px 5px 5px gray;
}
.list_item { cursor:pointer;
margin-left:10px;
color:red;
font-weight:bold;
}
.bullet_list { list-style-type:none;
margin-left:-50px;
}
.highlight_red { color:red; font-weight: bold; }
</style>
<body>
<div class="panel">
<h1>e-Shopping List</h1>
<input placeholder="Add shopping items here">
<button>Add</button>
<p>Press <span class="highlight_red">X</span> to delete a shopping item.</p>
<ul>
<li class="bullet_list"><span class="list_item">X </span>{{item}}</li>
</ul>
<p class="highlight_red">{{errorMessage}}</p>
</div>
</body>
Now that we have the standard framework in place, let’s add some dynamic content.
We will start first by:
Add the following highlighted SCRIPT code below the
<title>
tag and add the ng-app directive in the
<body>
tag:
WHY: To make the
<body>
tag of the HTML page an application.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>e-Shopping List</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
var app=angular.module("eShoppingListApp", []);
</script>
<style>
.panel{
width: 250px;
background-color: lightgray;
margin: 10px;
padding: 10px;
box-shadow: 5px 5px 5px gray;
-webkit-box-shadow: 5px 5px 5px gray;
}
.list_item { cursor:pointer;
margin-left:10px;
color: red;
font-weight: bold;
}
.bullet_list { list-style-type:none;
margin-left: -50px;
}
.highlight_red{ color:red; font-weight: bold;}
</style>
</head>
<body ng-app="eShoppingListApp">
NOTE: We have just associated the <body> tag with the AngularJS framework making the <body> tag and all of its content an application. How easy was that!!!
Now that we have created an app, let’s associate it with a controller to “control” our app. We want to add the following functionality to our controller:
After entering a "shopping" item:
Add the following highlighted code:
WHY: To assign a controller to the app and create a products array with a few default values to the current $scope.
<script>
var app=angular.module("eToDoListApp", []);
app.controller("eShoppingListCtrl", function($scope) {
// Default Items -------------------------------------------------
$scope.products=["Milk", "Bread", "Cheese"];
});
</script>
CODE EXPLANATION:
- The $scope.products is an array with three default items in it (Milk, Bread, and Cheese).
Now that we have associated the controller with the app within the SCRIPT code, we now need to associate the SCRIPT code within the HTML code.
<body ng-app="eShoppingListApp" ng-controller="eShoppingListCtrl">
<ul>
<li ng-repeat="item in products" class="bullet_list"><span class="list_item">x </span>{{item}}</li>
</ul>
Now that we know our application is working correctly, let’s add our first functionality. We will add an "addItem" function so that if a user click the Add button it will add the "shopping" item to the list with a "x" next to it.
<script>CODE EXPLANATION:
var app=angular.module("eShoppingListApp", []);
app.controller("eShoppingListCtrl", function($scope) {
// Default Item -------------------------------------------------
$scope.products=["Milk", "Bread", "Cheese"];
// Add function -------------------------------------------------
$scope.addItem = function(){
$scope.products.push($scope.addShoppingItem);
}
});
</script>
<div class="panel">CODE EXPLANATION:
<h1>e-Shopping List</h1>
<input ng-model="addShoppingItem" placeholder="Add shopping items here">
<button ng-click="addItem()">Add</button>
<p>Press <span class="highlight_red">x</span> to delete a shopping item.</p>
<ul>
<li ng-repeat="item in products" class="bullet_list"><span class="list_item">x </span>{{item}}</li>
</ul>
<p class="highlight_red">{{errorMessage}}</p>
</div>
Now that we can add items to the list, let's add code to remove items from the list.
<script>
var app=angular.module("eShoppingListApp", []);
app.controller("eShoppingListCtrl", function($scope) {
// Default Item -------------------------------------------------
$scope.products = ["Milk", "Bread", "Cheese"];
// Add function -------------------------------------------------
$scope.addItem = function () {
$scope.products.push($scope.addShoppingItem);
} // Remove Item --------------------------------------------------
$scope.removeItem = function (item) {
$scope.products.splice(item, 1); }
});
</script>
CODE EXPLANATION:
- The spice method of the products array is used to identify the item that need to be removed.
<li ng-repeat="item in products" class="bullet_list"><span ng-click="removeItem($index)"
class="list_item">x </span>{{item}}</li>
CODE EXPLANATION:
- The $index is the current item of the li element to ascertain which element to delete.
There are several issues that need to be resolved:
These problems can be resolved by adding some "if" conditional statements to check the value BEFORE adding it.
// Add function -------------------------------------------------
$scope.addItem = function (){
$scope.errorMessage = "";
if (!$scope.addShoppingItem) {return;}
if ($scope.products.indexOf($scope.addShoppingItem) == -1) {
$scope.products.push($scope.addShoppingItem);
} else {
$scope.errorMessage = "The item is already in your shopping list.";
}
}
CODE EXPLANTION:
- The
first "if" statement checks to see if no text was entered, if so, it will return without doing anything. Notice the NOT symbol (!) in front of the $scope.
- The second "if" statement checks to see if the item has not already been added. If not, add it to the products array and display it, else assign the message "The item is already in your shopping list" to the errorMessage placeholder ($scope.errorMessage).
// Remove Item --------------------------------------------------
$scope.removeItem = function (x) {
$scope.products.splice(x, 1); $scope.errorMessage=""; }
See Add Local Storage for example on how to implement Local Storage.