0% found this document useful (0 votes)
48 views

Webx M3 PDF

Uploaded by

Nagre Shitija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
48 views

Webx M3 PDF

Uploaded by

Nagre Shitija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 41
MODULE 3 Introduction to CHAPTER 3 AngularJS Uni ty Prescribed Syllabus ‘Academic Year 2021-2022 Overview of AngularuS, Need of AngularJS in real web sites, AngulaxJS modules, AngularJS bull in directives, AngularJS custom directives, AngularJS expressions, Angular JS Data Binding, AngularJS filers, AngularJS controllers, AngularJS scope, AngularJS dependency injection, Angular JS Services, Form Validation, Routing using 1g-Route, ng-Repest,ng-style, ng-view, Builtin Helper Functions, Using Angular JS with Typescript Sel sarning Toples: MVC model, DOM modal, Javascript functions and Error Hanaling 3.1 Overview of AngularsS 33 3.11 Features of AngulanJS: 33 3.1.2 Advantages of AngulardS 33 3.1.3 Disadvantages of AnguiarJS. 34 3.1.4 AngulardS Environment Setup. 34 8.2 Need of AngularJS in Real Websites: 36 88 AngularJS Modules. 37 34 Angular JS Buin Directives. 29 3.4.1 ngeapp. 39 3.42 ngdnit. an 3.4.3 ng-model an 3.4.4 ngsind 31 3.45. ng-tepeat 312 3.4.8 ngHt 312 34.7 ngereadonly 343. 34.8 ng-isabled 3.13 3.4.9 ng-cick 313 3.410 ng-show 344 3.4.11 ng-hide. 314 85 AngulatJS Custom Directives 315 3.6 AngularJS Expressions. 318 87 AngularJS Data Binding 3.19 3.7.1 One-Way Data Binding 3.19 3.7.2 Two-Way Data Binding 3.20 3.8 Angulards Filters. 3.20 3.8.1 Number Fier 3.21 38.2 Currency Fiter 321 383 Dale Filter 3.22 nracsemarisoeaborectomieen Ey studocu Downloaded by Nagre Shitja (vs4!2021007 @pvppcae.acin) Wob X.0 (MU-Sem 6-7) {ntroduotion to Angulax8)...Page no. (3-2) 384 UpporcaaTovercave Fer oa 385 Fier 320 388 order Fier 2 29 Angus Conor “sat 240 Angus Soope coin vn 885 310.1. Srootcope os aust Angas Dependency econ, oa ait Vate oar 2112. Farr, 227 8113. Sone oa S114 Prove oa S448 Constants oa 2.12 Angas Serves 320 a2 Ship ene, 320 8122. Slog Service oat 3128 Sintra! Service ose 12d Svan Sant vee 18. Fam Vakdaon.. vo {nn “38 aux4_Anguarss Roving 297 a4 ngRoute : vs nn 887 e142 noView oor 2142 StoueProvier ove 2.144 Angles Routing Syrian oes 148 Angus Routing Exampo ove 2.18 Buitin Helper Functions oe 3184 anqlareauals 309 8182 anglarfoon o40 162° angulacony oo 154 angulareSting.. “20 9185 angulrisaray. “340 886. angularmergen oat 2187 anglriDetnd and anguaisUndeined.ssssnnsnnnnnnnnnnnnnnnnnnnn del 9158 angular Dat oat 2188. anguar noon oat 2.18.10 angular Eten oat 2.18.11 angular Functon oa 28.12 anguaridnty oa 211519 angular orEch oa 2348.14 ang sNumber oa 2.15.18 anglais Object a8 8.1.18 angulsomon oa 8.18 Using Angas wth Typeset “a8 2.17 Anglard8 MVC Mel (Sa ering Top) ‘351 2.38 AnguatS HTML DOM (Sel Teaing Top) ‘331 + Chapter Ends. 352 (MU-New yes we academic year 21-22) M6105) I rech-reoPubatons.A SACHIN SHAH Vertue Downloaded by Nagre Shit (wes!2021097 @pvppcoe.acin) Wb X.0 (MU-Sem 6-17) {introduation to AngulardS)...Page no. (3-3) 3.1 OVERVIEW OF ANGULARIS © AngulardS is a client side JavaScript MVC framework to develop a dynamic web application © Angulary$ was originally started as a project in Google but now, itis open source framework. © Angulary$ is entirely based on HTML and JavaSerip so there is no need to learn another syntax or language, ‘© AngularJS is also called just "Angular" © AngularJS changes static HTML to dynamic HTML. It extends the ability of HTML by adding builtin attributes and component and also provides an ability to create custom attributes using simple JavaScript. © AngulasdS can be used to create Applications. Single Page © AngularJS website - https //angularjsorg 3.1.1 Features of AngularJs (a) General Features ‘The general features of AngularJS are as follows © AngularJS is an efficient framework that can create Rich Internet Applications (RIA), © AngulardS provides developers an options to “tite client side applications using JavaScript in a clean Model View Controller (MVC) way. © Applications written in Angular JS are cross- browser compliant. Angula:JS automatically handles JavaScript code suitable for each browser. © AngulaiIS is open source, completely free, and used by thousands of developers around the world. It is licensed under the Apache license version 2.0. © Overall, AngularJS is a framework to build large scale, high-performance, and easy-to-maintain web applications (b) Core Features ‘The core features of AngularyS are as follows: (2) Data-binding : Its the automatic synchronization of data between model and view components (2) Scope : These are objects that refer to the model ‘They act as a glue between conteoller and view. (3) Controller : These are JavaScript functions bound {o a particular scope. (A) Services AngularJS comes with several builtin Shup XMLHupRequests. These are singleton objects such as make a which are instantiated only once in app, (S) Filters : These select a subset of items from an array and returns a new array (©) Directives elements such a8 elements, atuibutes, css, Ditectives are markers on DOM and ‘more. These can be used to create custom HTML tags that serve as new, custom widgs Angular JS has builtin directives such as ng-Bind, ng- Model, ee (D) Templates information from the controller and model. These ‘These are the rendered view with can be a single file (such as index html) or Module multiple views in one page using partials. (8) Routing Ivis concept of switching views. (9) Model View Whatever : MVW is a design pattern for dividing an application into different parts called Model, View, and Controller, each with distinct responsibilities. Angular JS does not implement MVC in the traditional sense, but zather something closer to MVVM (Model-View- View-Model). The Angular JS team refers it humorously as Model View Whatever, (10) Deep Linking : Deep linking allows to encode the state of application in the URL so that it can be bookmarked. The application can then be restored. {rom the URL. tothe samme state, (11) Dependeney Injection : AngularJS has a builtin dependency injection subsystem that helps the developer to create, understand, and test the applications easly. 3.1.2 Advantages of Angular]S ‘The advantages of AngulasIS are (1) Ie provides the capability to create Single Page Application in a very clean and maintainable way Q) Ie provides data binding capability to HTML. Thus, it gives user a rich and responsive experience. 3) _AngulartS code is unit testable (MU-New Syllabus wes academic year 21-22) (M6-104) IB rechneo Publications. SACHIN SHAH Venture receamisoaibionestcnenon EY studocu Downlaadad by Nogre Shibja(vut!2021007 @pvpnco8.20.n) 3 Wb .0 (MU-Sem 6-17) {ntroduotion to Angulan8)...Page no. (3-4) (A) AngularyS user dependency injection and make use of separation of concerns. (5) AngulatdS provides reusable components (6) With Angula:IS, the developers can achieve more functionality with short code. (1D In AngularlS, views are pure html pages, and controllers written in JavaScript do the business processing. (8) AngularJS applications can run on all major browsers and smart phones, including Android and iOS based phonesitablets, 3.1.3 Disadvantages of Angular}s ‘Though AngularTS comes with a lot of merits, here are some points of concer (1) Not Secure : Being JavaScript only framework, application written in Angular JS ate not safe. Server side authentication and authorization is must to Keep an application secure, (2) Not degradable : If the user of your application disables JavaScript, then nothing would be visible except the basic page. 3.1.4 Angular]S Environment Secup We need the following tools to setup a development environment for AngularJS () Angulels Library @)_ Web server (4) Browser > (2) Angularss Library To download Angula:IS library, go to angulayjs.org -> click download button, which will open the following popup. Download AngularJS (MU-New Syllabus wes academic year 21-22) (M6-104) Download Angular3s Library ‘© Select the required version from the popup and click on download button in the popup. © CDN: You can include AngularJS library from CDN bupsf/ajax.googleapis.com/ajan/libslangulars/l.3.16/a gular min js > (2) Editor Angular}S is eventually HTML, and JavaScript code. So, you can install any good editor/IDE as per your choice. The following editors are recommended (1) Sublime Text (3) Ula Bai (5) Visual Stadio (2) Aptana Studio 3 (@) Belipse Online Editor You can also use the following online editors for learning purpose, + plntr.co © jsbincom > (3) Web server Use any web server such as HS, apache etc, locally for evelopment purpose > (4) Browser You can install any browser of your choice as AngularJS supports cross-browser compatibility. However, itis recommended to use Google Chrome while developing an application Angular Seed Use Angular seed project to quickly get started on AngularJS application. ‘The Angular-seed is an application skeleton for @ typical AngularJS web application. You can use it to quickly bootstrap your angular webapp projects and development environment for your project. © Download angular-sced from GitHub Lets setup Angular project in Visual Stadio 2013 for web IB recto ubctons. A SACHIN SHAW Verte Downloaded by Nagre Shitja ([email protected]) Wob X.0 (MU-Som 6-7) (ntroduation to Angulard8)...Page no. (3-5) Setup Angular3S Project in Visual Studio + You can create Angulas$ application in any version of Visual Studi. Here, we will se Visual tuo 2013 for web ist, create new project by clicking on New Project link on start page. This will open New Project dialog box, as shown below. Module Angular3Js in Visual Studio Select Web in the left pane and ASP.NET Web Application in the middle pane and then click OK. In the New ASP.NET Project dialog box, select Empty template and then click OK This will create an empty website project in Visual Studio, + Now, install AngulatYS library from NuGet package manager. Right click on the project in Solution Explorer and select Manage NuGet Packages. (MU-New Syllabus wes academic year 21-22) (M6-104) IB rechneo Publications. SACHIN SHAH Venture receamisoaibionestcnenon EY studocu Downlaadad by Nogre Shibja(vut!2021007 @pvpnco8.20.n) Wb .0 (MU-Sem 6-17) {ntroduation to Angulax8)...Page no. (3-6) Seton Spier=r © e@|e-aagial r= Sear Solubon Esplver Cul) ° *P Ac Application Insights Terai Scope to Ths aad . Manage NuGet Packages, Debug » + oe K acts cue ax ‘© Search for “angular” in the Manage NuGet Packages dialog box and install AngularJS Core, ce This will add AngulayS files into Seripts folder such as angularjs, angularaminjs, and angular-mocksjs, as follows: e©e@) o-alr— Search Solution Explorer (Ctrl) Db & Properties > em References 4 Scripts TT angular mocks: SF angularis F angulocmminjs 1) angularmin.jsmap SD packages.config > A Web.contig ‘© Now, you can start writing AngularJS web application. 13.2 NEED OF ANGULARJS IN REAL WEBSITES ‘The most valuable reasons of AngularJS usefulness lie in their concepts and benefits that they provide, However, documentation and wide community also give a point to Angular usage. The major reasons for using Angulaels in real websites include (1) Major community : It opens a large scale of options both to developers and clients. Developers may easily find various info and solutions. Clients have wider choices of development teams (2) Readable code : While code is more understandable to readers itis a way easier to maintain, Moreover, clients don’t bind to developers in this case and have minimum problems if change programmers that maintain it (8) Google as creation: Someone may say it isn’t a direct benefit of AngularJS. However, it is heavily supported. (4) Customizable : Developers don't require to use all libraries and even may change them, As a result, the size ofthe framework may be heavily reduced () Flexible : Filters allow distilling data before it gets into view. So, it helps to deal with pagination, formatting and reversing a text, (MU-New Syllabus wes academic year 21-22) (M6-104) IB rech-neoPubcations.A SACHIN SHAH Venture Downloaded by Nagre Shitja ([email protected]) Wb X.0 (MU-Sem 6-17) (troduction to Angulard8)...Page no. (3-7) (6) Fast for small ; While any large and complex web app Angular]S developing small apps really fast. In addition, it also may be used to develop large projects lakes much time to develop allows (1) Charge dependency : Dependency injection will help you manage back-end, In the end, web app becomes faster and more stable (8) Premade solutions: AngularJS contains a bunch of different solutions ready to use in the app routing modules and much more, (9) Easy testing : AngularJS allows to write and test module code separately ftom the rest of an app. As a resull, only necessary parts of apps and chosen services will be tested D1 3.3_ANGULARJS MODULES © A module in AngularJS is a container of the different parts of an application such as controller, service filters, direotives, factories ete ‘© Ic supports separation of concern using modules. + Modules are used to separate logic such as services, controllers, application ete. from the code and maintain the code clean, We define modules in separate js files. © A module is used as a Main() method, Example 1 : In the below example, the angular module() method cxeates an application module, where the first parameter is a module name Which is same a specified by ng-app dizective, The second parameter is an array of other dependent modules []. In the above example we are passing an empty array because there is no dependency [Note : The angular.module() method returns specified module object if no dependency is specified. Therefore, specify an empty array even if the current module is not dependent on other module, © Now, you can add other modules in the myApp module, ©The below example also demonstrates creating controller module in myApp module. We have created a controller ‘myController” using myApp.controller() method. Here, myApp is an object of a module, and contuoller() method creates a controler inside "myApp” module. So, "myContraller’ named ‘will not become a global function

AngularIS Module Demo: ‘myControler! >
yyApp = angular.module("myApp", [)s smyApp.controller("myContrller’ function scope) { ‘Sscopemessuge = "Hello Angular World!" Maile » 3 < Output : AngularJS Module Demo: Hello Angular World! In the shove example, we created application module and controller in the same HTML. file, However, we can create separate JavaSeript files for each module as shown below. <[body> (MU-New Syllabus wes academic year 21-22) (M6-104) IB rechneo Publications. SACHIN SHAH Venture nracsemarisneaborectomieen Ey studocu Downlaadad by Nogre Shibja(vut!2021007 @pvpnco8.20.n) Wb .0 (MU-Sem 6-17) {itroduation to Angulax8)...Page no. (3-8) File Name: apps var myApp = angular.module("myApp", )) File Name: myController,js ‘myApp.controler("myContrller,funetion (Sscope) { Secopesmessage ~ "Hello Angular World!"; Ys Example 2 : Here, we use application module using ng- app ditective, and conuoller using ngcontoller directive. We import the mainApp.js and studentControllerjs in the ‘main HTML page. In mainApp.s we declare an application mainApp module using angular.module function and pass aan emply array to it. This array generally contains dependent modules. In studentControllerjs, we declare a controller studentController module using mainApp.controller function, File Name: testAngularJS html Angular JS Modules

AngularJS Sample Application

“div ng-app = "mainApp" ng-contoller = 'studentControlle"> ‘
Enter first name: ‘student. frstName"> Enter last name:
Name: {student fllNameQ)})} Subject:
Name Marks
{{ subjectiname }) {{ subjectamarks }}
File Name: mainApp.is var mainApp = angular.module(’mainAp []}; File Name: studentController js mainApp.controler('studentController, funetion($seope) { Sseope.student = { firstNames "Sach (MU-New Syllabus wes academic year 21-22) (M6-104) IB rech-neoPubcations.A SACHIN SHAH Venture Downloaded by Nagre Shitja ([email protected]) Wb X.0 (MU-Sem 6-17) (itroduation to Angulard8)...Page no. (3-9) TastName: "Shab, foes:500, subject:[ {name: Physics! marks:70}, {name: Chemistry’ marks:80), {name:Math’smarks:65}, with ng- where ng stands for Angular. AngularJS includes various buil-in directives. In addition to this, you ean create custom ditectives for your application. The Table 3.4.1, lists the important built-in Angular] Gixectives, {name:'English'marks:75}, Table 3.4.1 : {name:'Hindi'ymarks:67} Directive Description fullNames function) { nea ‘Auto bootstrap AngularIS application, en ng-init Initializes AngularJS variables student Object = Sscope.student Fears lolenObjoctfestNemo + "* ngmodel | Binds HTML controls value to studentObject.lastName; property on the Sscope object. , ng-controller | Attaches the controller of MVC to the hh view. » ng-bind Replaces the value of HTML contol output : with the value of specified AngularIS ae expression, AngularJS Sample Application ngepeat | Repeats HTML vemplate once per each stem inthe specified collection, ey ee ng-show Display HTML element based on the ‘ifeclost names. || Shan value ofthe specified expression, nereadonly | Makes HTML. element read-only based Name Sachin Sha ean | enthe value of th pein expresion ‘Name ng-disabled ‘Sets the disable attribute on the HTML clement if specified expression evaluates Physics to true, su Chemistry net Removes or recreates HIML clement Subject based on an expression Math * ng-click | Specifies custom behavior when an eae clement is clicked 3.4.1 ng-app 13.4 ANGULAR JS DIRECTIVES © Directives are markers on a DOM element that tell AngulatJS to attach a specified behavior to that DOM. clement or even transform the DOM element and its children, In shor, it extends the HTML © Most of the directives in AngularIS are starting The ng-app directive is a starting point of AngularJS Application. I; initializes the AngularSS. framework automatically AngularJS framework will first check for ng-app Guective in a HTML document alter the entice document is loaded and if ng-app is found, it bootstraps itself and compiles the ITTML template (MU-New Syllabus wes academic year 21-22) (M6-104) IB rechneo Publications. SACHIN SHAH Venture nracsemarisneaborectomieen Ey studocu Downlaadad by Nogre Shibja(vut!2021007 @pvpnco8.20.n) Module Wb .0 (MU-Sem 6-17) (Introduction to AngularJS)...Page no. (3-10) Typically, ag-app directives should be placed at the root of an HTML document e.g. or tag, so that it can control the entire DOM hierarchy However, you can place it in any DOM element. The AngularlS framework will only process the DOM clements and its child clements where the ng-app directive is applied © Example : In the below example, ng-app directive is placed in the div element whose id is. "myDiv ‘Therefore, AngularJS will only compile myDiv and its child elements. It will not compile the parent or sibling clements of myDiv, ‘ < ng-app Directive</itle> <script sro="https(/ajax googleapis.com/ajax/libslangularjs/1.3.16/en gularmin,js"> </script> <head> <body > <div> 4212} </div> <div id="myDiv" ng-app> (5/24) <div> 4410/2} </div> </div> <div> 44222} </div> <Sbody> </ul> output 4{(2i2)) 25 5 {22}) ‘The Fig. 3.4.1 illustrates the above example Note that multiple ng-app directives are NOT allowed in single HTML document. “IDOCTYPE him> Sti heads “tilo>ng-app Diroctivesitlo> <sotipt sro~ hil ajax qooglespis, com’ ajax! Iibs/angulajs/1-3.16/angular.minjs'><Iscript> nt oom "xo a athe? See myo weap en oe A ‘supported <a only inside ae ed ea 272), “lah <ody> Simi ‘ocnig, 34.1 : Bootstrapping of ng-app ‘ng-app with Module name The specify an application module name. This application module separates Gifferent parts of your application such as controllers, services, filters ete ng-app directive can also Example : In the above example, we have specified a module name using ng-app = ‘myAngularApp'in the <body> tag, and then we have created ‘myAngularApp ‘module using angular.module() Function inside <script <IDOGTYPE himl> <biml> <head> <itle>ng-app Direetive
Enter your name:
Hello output 10 Enter your name: Hello iy Enter your name: [Sachin Hello Sachin ‘© In the above example, ng-bind directive binds a result of an expression "10 + 5" to the . The same ‘way, itbinds a value of a model property "name" to the . The value of "name" property will be the value entered in a textbox. 3.4.5 ng-repeat © The ng-repeat directive repeats HTML once per each item in the specified atray collection Example: ‘ in students">
width: 100%; height: 50px3, display: blocks margin: 15px 0.0 10px; }
Read-only:
Disabled:
output (click Me: O New: ‘Read-only: Tiss road-ony Disable: Msi asabied ———] Click Me: New} Read-only [This is read-only Module Disabled: [This i disabled W349 ng-click ©The “ng-lick directive” in AngulaeIS is used to apply ceustom behavior when an clement in HTML is clicked ‘This directive is normally used for butions because that is the most commonplace for adding events that respond to clicks performed by the user. It is also used to popup alerts when a button is clicked Event Registration

AngularJS ng-click Directive Example {jax/libs/angularjs/1.3.16/an <