Docsity

Angular and Web Development Part 2, Study notes of Engineering

Partial preview of the text

Download Angular and Web Development Part 2 and more Study notes Engineering in PDF only on Docsity!

Angular and

Web

Development

Part 2

SWEN- 261 Introduction to Software Engineering Department of Software Engineering Rochester Institute of Technology

Summary

  • Angular is a component-based framework that is used for developing single page applications employing TypeScript and HTML template language - Typescript is a language that compiles to JavaScript. It is strongly typed, object oriented and compiled language - HTML (Hypertext Markup Language) is the code that is used to structure a web page and its content - CSS (Cascading Style Sheets) is the language we use to style a Web page
  • Last class we covered Modules and Components
    • Modules are objects that help you to organize dependencies into discrete units
    • Components are new elements that will compose the majority of your application’s structure and logic

Angular – What’s next

  • Data binding
  • Services
  • Routing
  • Observables

Angular – Data Binding

  • Data binding automatically keeps your page up-to-date based on your application's state. You use data binding to specify things such as the source of an image, the state of a button, or data for a particular user
  • There are four types of data binding available in Angular:
    • Event binding - This data binding type is when information flows from the view to the component when an event is triggered
    • Interpolation - Text representing variables in components are placed in between double curly braces in the template
    • Two-way data binding - Two-way binding is a mechanism where data flows both ways from the component to the view and back
    • Property binding - Property binding is a one-way mechanism that lets you set the property of a view element

Data Binding – Interpolation

  • Interpolation is used for one-way data binding
  • It moves data in one direction from our components to HTML elements
  • Angular evaluates the expressions into a string and replaces it in the original string and updates the view
  • Angular uses the {{ }} in the template to denote the interpolation
  • Examples:

Data Binding – Two-way Data Binding

  • Two-way data binding in Angular will help users to exchange data from the component to view and from view to the component
  • It will help users to establish communication bi-directionally.
    • If a property in the component is changed that change flows to the view
    • Same way change in view is reflected in the bound property in the component

Data Binding – Property Binding

  • Property binding in Angular helps you set values for properties of HTML elements or directives
  • Use property binding to do things such as toggle button functionality, set paths programmatically, and share values between components
  • Property binding moves a value in one direction, from a component's property into a target element property
  • To bind to an element's property, enclose it in square brackets, [], which identifies the property as a target property Target property

Angular – Data Binding Example

  • Using Data binding, we can pass data between the component and template Property binding Event binding greet.component.html greet.component.ts Two-way data binding Interpolation

Angular Example – Create Service

ng g service log

  • Use the Angular CLI to generate a service for a simple logger

New log method

Angular Example – Service Details

@Injectable() decorator to provide the metadata that allows Angular to inject it into a component as a dependency log.service.ts

  • Add a new log() method to log messages to the console greet.component.ts The log service is injected into the greet component

Routing

  • Most applications require the ability to navigate between different pages during the lifecycle of the application.
  • Typically, an application has at least a few basic pages, such as a login page, home page, user’s account page, and so forth.
  • Routing is the term used to describe the capability for the application to change the content on the page as the user navigates around.
  • The Angular router is a core part of the Angular platform

Routing

  • In Angular, the best practice is to load and configure the router in a separate, top-level module that is dedicated to routing and imported by the root AppModule
  • Use the Angular CLI to generate ng generate module app-routing --flat --module=app Puts the file in src/app register it in the imports array of the AppModule.

Routing

  • Our new start page links to other pages app.component.html The tells the router where to display routed views

Observables

  • Observables provide support for passing messages between parts of your application
  • They are used frequently in Angular and are a technique for event handling, asynchronous programming, and handling multiple values
  • For example, consider requesting data from a server via HTTP
    • If the content was retrieved synchronously (following the request), the browser could freeze the UI while it waited for the server's response
    • Instead, we want to be notified when when the content is available