Top Banner
NOTES AND TUTORIALS LAB BY: JONATHAN MCCARTHY ANGULARJS
31

ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

Oct 02, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

N O T E S A N D T U T O R I A L S L A B B Y : J O N A T H A N M C C A R T H Y

ANGULARJS

Page 2: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

INTRO TO ANGULAR SECT ION 1

Slides by: Jonathan McCarthy 2

Page 3: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

HISTORY OF ANGULAR

•  AngularJS was created by Misko Hevery and Adam Abrons in 2009. Abrons left the project.

•  Misko Hevery worked at Google and continued working on Angular. Misko offered Angular as a possible solution for a problem they were having on a project named Google Feedback.

•  Misko estimated that the entire project could be re-written in two weeks using Angular. If took 3 weeks and the lines of code went from 17,000 to 1,500. •  Ref: O’Reilly, AngularJS (Book)

Slides by: Jonathan McCarthy 3

Page 4: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

OVERVIEW

•  AngularJS is a JavaScript Framework.

•  AngularJS is a structural framework used to develop dynamic web applications.

•  It uses HTML as the basis for its templating and provides the ability to extend HTML to work easily with Angular.

•  Angular uses data binding and dependency injection to add content to the application.

•  Everything happens within the browser, eliminating and dependency on server side technologies.

Slides by: Jonathan McCarthy 4

Page 5: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

OVERVIEW

•  Angular can interact with the DOM and make AJAX requests from the server.

•  Angular follows a well defined structure and follows an opinionated convention on how certain tasks should be performed.

•  Angular provides the following functionality to create simple CRUD apps out of the box: •  Data-binding •  Templates •  form validation •  deep-linking •  reusable components •  dependency injection

Slides by: Jonathan McCarthy 5

Page 6: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

MVC

•  Trygve Reenskaug when working at the Xerox Palo Alto Research Laboratory (PARC), created the pattern known as MVC in 1979.

•  Model–view–controller (MVC) is a software design pattern. •  It is an architecture pattern to separate the representation of

information from the user's interaction with it. •  The business logic of the web application resides on the

model. •  The controller acts as a mediator between the models and

views. •  The views primary role is the presentation of information to the

end users. •  Angular follows the MVC pattern (MVC \ MVVM \ MV*)

Slides by: Jonathan McCarthy 6

Page 7: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

WHY USE MVC?

•  MVC implements Separation of Concerns (SoC). •  Separating the business logic from the presentation code

allows for better testing of the application. (TDD) •  By separating functionality between the Model, View, and

Controller allows each section to be tested independently. •  Cleaner presentation code (HTML, CSS, JavaScript), with

minimal code nuggets. •  MVC offers structure to a web application. •  By using this structure in a team environment allows everyone

to work off “the same hymn sheet”. •  It provides common features and functionality to developers

in an environment with a common structure. •  MVC model is using REST based URLs instead of specific file

names (eg. default.aspx, index.php)

Slides by: Jonathan McCarthy 7

Page 8: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

WHY USE ANGULARJS

•  Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality and structure required to develop an application.

•  As with all libraries and frameworks, they are only suitable for certain types of projects.

•  Applications that need CRUD functionality with no specific requirement for complex DOM manipulation, then AJAX will be a good fit.

Slides by: Jonathan McCarthy 8

Page 9: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

ANGULAR PHILOSOPHY

•  Decouple DOM manipulation from business logic

•  This makes it easier to test the application

•  Distinct separation of client side and server side

•  Framework offers common functionality to developers in a structured manner

•  Most of the tedious tasks of manipulating the DOM have been taken care of by Angular.

Slides by: Jonathan McCarthy 9

Page 10: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

ANGULAR TERMINOLOGY SECT ION 2

Slides by: Jonathan McCarthy 10

Page 11: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

TEMPLATES

•  Standard web apps generate a html response that comprises of the data in the html structure.

•  Angular passes the template and the data to the browser for assembly.

•  The template is the html document with some extensions that are added to the syntax. An example of an extension is the addition of new attributes for data binding etc (see next slide)

•  Data can be passed to the page for processing and display.

Slides by: Jonathan McCarthy 11

Page 12: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

DIRECTIVES

•  Angular uses HTML as the template. •  It includes a DOM transformation engine that is

used to extend the HTML syntax. •  A directive is a marker that is added to a DOM

element. Eg: •  Element •  Attribute •  This is used by the DOM transformation engine to

attach a specific behaviour to a DOM element. •  Angular provides a set of pre-defined directives.

Slides by: Jonathan McCarthy 12

Page 13: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

DIRECTIVES

•  Angular provides a set of pre-defined directives. Eg: •  ngApp •  ngBind •  ngModel •  ngController •  ngClass •  ngRepeat

•  It is possible to create your own directives

Slides by: Jonathan McCarthy 13

Page 14: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

DIRECTIVES ( NG-APP)

•  The ng-app directive tells angular which element block of the page it will be managing. Eg. If we wanted Angular to manage the entire page the template would contain the following starting html element: •  <html ng-app>

•  If we wanted Angular to manage a subset of the page, the ng-app attribute could be placed on a different element on the page.

Slides by: Jonathan McCarthy 14

Page 15: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

DIRECTIVES ( NG-BIND)

•  Ng-bind tells angular what data or expression should be used to replace the existing content of a given element. This can also be updated when the data or expression changes. Double curley brackets can be used to bind data {{ }}. Ng-bind is preferred, as this in invisible as part of a page load, the double curley brackets may show!!

Slides by: Jonathan McCarthy 15

Page 16: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

DIRECTIVES ( NG-MODEL)

•  The ngModel directive binds user input to a property on the scope using NgModelController. •  User Input can be an input,select, textarea (or

custom form control) etc…

Slides by: Jonathan McCarthy 16

Page 17: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

MODEL

•  The model is responsible for managing application data. It responds to the request from view and to the instructions from controller to update itself.

Slides by: Jonathan McCarthy 17

Page 18: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

CONTROLLER

•  In Angular, the controller is our JavaScript classes. •  JavaScript functions will perform the role of the

controller.

•  The controller can request functionality from the model.

•  The controller can send and receive to\from the views.

Slides by: Jonathan McCarthy 18

Page 19: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

VIEW

•  What can we do in a View? •  Display Data •  Loop through a result set •  Get user input •  Display messages •  Links to certain functionality •  Structuring Layouts

Slides by: Jonathan McCarthy 19

Page 20: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

SECTION 3 EXAMPLES

Slides by: Jonathan McCarthy 20

Page 21: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

HELLO WORLD EXAMPLE

•  The following example will be explained in class!!

•  If you have missed class please follow the online tutorials that have been added to Moodle.

Slides by: Jonathan McCarthy 21

Page 22: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

HELLO WORLD EXAMPLE

•  Create a new page named hello.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title of the document</title> </head> <body> <h1>Angular Hello World!!</h1> </body> </html>

Slides by: Jonathan McCarthy 22

Page 23: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

ANGULAR SCRIPT

•  Add Angular to the page from the Google CDN

•  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

•  Add the above script to the head section.

Slides by: Jonathan McCarthy 23

Page 24: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

ADD OUR OWN JS FILE

•  Create a new assets folder in your project folder •  In the new assets folder, create a new js folder. •  Create a new file named controller.js in the js folder.

•  Add the following to the head section: •  <script src=“/assets/js/controller.js”></script>

Slides by: Jonathan McCarthy 24

Page 25: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

ADD THE ANGULAR MARKUP TO THE PAGE

•  Update your page to look as follows: <html ng-app="myFirstApp"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src="assets/js/controller.js"></script> </head> <body> <div ng-controller="helloController"> <p>{{greeting.text}}, world </p> <p><span ng-bind="greeting.text"></span> Jono, hope this works for you!!</p> </div> </body> </html>

Slides by: Jonathan McCarthy 25

Page 26: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

CONTROLLER.JS

•  Add the following code to controller.js var myFirstApp = angular.module('myFirstApp', []); myFirstApp.controller('helloController', ['$scope', function($scope) { $scope.greeting = { text: 'Hello' }; }]);

Slides by: Jonathan McCarthy 26

Page 27: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

BIG PICTURE – HELLO WORLD

var myFirstApp = angular.module('myFirstApp', []); myFirstApp.controller('helloController', ['$scope', function($scope) { $scope.greeting = { text: 'Hello' }; }]);

// Business logic, if we had some!!

<html ng-app="myFirstApp"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src="assets/js/controller.js"></script> </head> <body> <div ng-controller="helloController"> <p>{{greeting.text}}, world </p> <p><span ng-bind="greeting.text"></span> Jono, hope this works for you!!</p> </div> </body> </html>

Slides by: Jonathan McCarthy 27

Page 28: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

EXAMPLE 2 – STUDENT LIST

•  Create a page to display a students information and their grades for semester 2. •  Firstname •  Lastname •  Student Number •  Email •  Grades

•  Note: •  The following example will be explained in class!! •  If you have missed class please follow the online tutorials

that have been added to Moodle.

Slides by: Jonathan McCarthy 28

Page 29: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

STUDENTS – HTML FILE

•  Create a new file named students.html <html ng-app="myStudentApp"> <head> <title>Your Shopping Cart</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src="assets/js/students.js"></script> </head> <body ng-controller='studentController'> <h1>Student Details</h1> <p>{{student.firstname}}</p> <p>{{student.lastname}}</p> <p>{{student.studentNum}}</p> <p>{{student.email}}</p> <h2>Grades</h2> <div ng-repeat='i in grades'> <span>{{i.module}} - {{i.grade}}</span> <button ng-click="remove($index)">Remove</button> <input ng-model='i.grade'> </div> </body> </html>

Slides by: Jonathan McCarthy 29

Page 30: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

STUDENTS – STUDENTS.JS

•  Create a new file named students.js in the assets/js folder

var myStudentApp = angular.module('myStudentApp', []); myStudentApp.controller('studentController', ['$scope', function($scope) { $scope.student = { firstname: 'Johnny', lastname: 'McCarthy', studentNum: 123456, email: '[email protected]' }; $scope.grades = [ {module: 'Advanced Client Side Development', grade: 60}, {module: 'Cloud Computing', grade: 67}, {module: 'Programming 101', grade: 80} ]; $scope.remove = function(index) { $scope.grades.splice(index, 1); }; }]);

Slides by: Jonathan McCarthy 30

Page 31: ANGULARJSamitycode.com/tutorials/Intro_to_Angular.pdfWHY USE ANGULARJS • Angular helps simplify the development process by offering a higher level of abstraction for the basic functionality

ADDITIONAL LAB WORK

•  See Moodle for additional lab work!!

Slides by: Jonathan McCarthy 31