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-NotePad</title>
</head>
<body>
</body>
</html>
Now that we have a framework established, let’s add static view components to it.
<body> <div> <h1>e-NotePad</h1>NOTE: This is standard HTML code. There is nothing dynamic here.
<textarea cols="40" rows="10"></textarea>
<br/><br/>
<div>
<button>Save</button>
<button>Clear</button>
<p>Number of characters left: 100</p>
</div> </div> </body>
Now that we have created the basic HTML content, let's "style" the application with some CSS goodness.
body {
font-family: Arial;
}
/* Panel styles ---------------------------------------*/
.panel {
width: 320px;
background-color:antiquewhite;
margin: 10px;
padding: 20px;
-webkit-box-shadow: 5px 5px 5px gray;
box-shadow: 5px 5px 5px gray;
border-radius: 10px;
}
/* App Title styles -------------------------------- */
.app_title{
text-align: center;
border-radius: 25px;
background-color:blue;
color: white;
border: 1px solid gray;
}
<link href="e-NotePad.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="panel">
<h1 class="app_title">e-NotePad</h1>
...
</div>
Now that we have the standard framework in place, let’s add some dynamic content.
We will start first by:
Write the following highlighted SCRPT code below the
<title>
tag and 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-NotePad</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
var app=angular.module("eNotePadApp", []);
</script>
</head>
<body ng-app="eNotePadApp">
<h1>e-NotePad</h1>
<textarea cols="40" rows="10"></textarea>
<br/><br/>
<div>
<button>Save</button>
<button>Clear</button>
<p>Number of characters left:</p>
</div> </body>
</html>
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 text:
Write the following highlighted code:
WHY: To assign a controller to the app and to give it a variable name of message with a temporary default value of “Hello, World.”
<script>
var app=angular.module("eNotePadApp", []);
app.controller("eNotePadCtrl", function($scope) {
$scope.message="Hello, World";
});
</script>
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="eNotePadApp" ng-controller="eNotePadCtrl">
<body>
<h1>{{message}}</h1>
Now that we know our application is working correctly, let’s add our first functionality. We will add a clear function so that if a user click the Clear button it will clear the content of the note in the textarea.
<script>
var app=angular.module("eNotePadApp", []);
app.controller("eNotePadCtrl", function($scope) {
$scope.message=""; // Clear Function ----------------------------
$scope.clear=function() {
$scope.message="";
};
});
</script>
<button ng-click="clear()">Clear</button>
<textarea ng-model="message" cols="40" rows="10"></textarea>
This is an optional functionality that you may want to add if you want to inform a user as to how many characters is left to type. Otherwise, you can delete the “Number of characters left:” paragraph from your application and skip these steps.
<script>CODE EXPLANATION: The return 100 - $scope.message.length will “return” the current number of characters subtracted from the total (in our case, 100). The length property ascertain how long the message is by the number of characters. NOTE: You can change the 100 to whatever value you want to.
var app=angular.module("eNotePadApp", []);
app.controller("eNotePadCtrl", function($scope) {
$scope.message="";
// Clear Function ----------------------------
$scope.clear=function() {
$scope.message="";
};
// Left Function ---------------------------- $scope.left=function() {
var totalcharacterleft=$scope.message.length
return 100-totalcharacterleft;
};
});
</script>
<p>Number of characters left: <span ng-bind="left()"></span></p>
Now let’s add Save button to our application.
<script>
var app=angular.module("eNotePadApp", []);
app.controller("eNotePadCtrl", function($scope) {
$scope.message="";
// Clear Function ---------------------------- $scope.clear=function() {
$scope.message="";
};
// Left Function ---------------------------- $scope.left=function() {
var totalcharacterleft=$scope.message.length
return 100-totalcharacterleft;
};
// Save Function ---------------------------- $scope.save=function() {
alert("Your note has been saved.");
}; });
</script>
<button ng-click="save()">Save</button>
See Add Local Storage for example on how to implement Local Storage.