Angular Js
Angular Js
Dr. Sandhya P
Professor Grade1
SCOPE
VIT-Chennai
What is Angular?
• Angular is a javascript framework that allows you to create reactive
single page applications (SPA).
C:\Users\cathe\Desktop\Angular\my-first-app\src\app
Angular Application
• Angular application has an index.html
• It has a special tag called <app-root>
• This element is used to insert components.
• Components are a central building block of Angular
applications.
• Each component is made up of a TypeScript class,
HTML, and CSS.
• TypeScript transpiles, or converts, into JavaScript, which
means that your application ultimately ends up in plain
JavaScript but you have the convenience of using
Typescript's extended features and streamlined syntax.
Angular Application
• To change the UI dynamically use directives. Ex: ngIf, ngFor (Actions
are performed on DOM)
• Data can be shared between the components using decorators.
• You use these decorators to specify that certain properties allow data
to go into or out of a component.
• To use @Output(), you raise an event in one component so that the
other component knows that there is data available.
Example Application – todo app
• Define Item in app directory (item.ts)
• Creates Item object model
Example Application – todo app
• Add logic to AppComponent (Edit app.component.ts)
app.component.ts
• import { Component } from '@angular/core’; //imports Angular
• @Component({
selector: 'app-root’,
templateUrl: './app.component.html’,
styleUrls: ['./app.component.scss’]
})
//metadata about AppComponent
metadata about AppComponent
Getter method
Example Application – todo app
• Add HTML to the AppComponent template (edit app.component.html)
ngFor directive
• The <li> contains an *ngFor, a built-in Angular directive that iterates
over the items in the items array.
• For each item, *ngFor creates a new <li>.
• The double curly braces that contain item.description instructs
Angular to populate each <li> with the text of each item's description.
Example Application – todo app
• Add Items to the list (edit add.component.ts)
• The addItem() method takes an item that the user provides and adds it to the array when
the user clicks the Add button.
• The addItem() method uses the array method unshift() to add a new item to the beginning
of the array and the top of the list.
Example Application – todo app
• To use addItem() edit (app.component.html)
app.component.html
• In the HTML file, #newItem is a template variable.
• The template variable in this case uses the <input> element as its value.
Template variables can be referred to anywhere in the component's template.
• When the user types a new item in the <input> field and presses Enter, the
addItem() method adds the value to the allItems array.
• Pressing the Enter key also resets the value of <input> to an empty string.
• The template variable #newItem is used to access the value of the <input>
element in the template.
• Instead of pressing the Enter key, the user can also click the Add button,
which calls the same addItem() method.
Example Application – todo app
• Styling the Angular App
• The Angular CLI generates two types of style files:
• Component styles: The Angular CLI gives each component its own file for
styles. The styles in this file apply only to its component.
• styles.css: In the src directory, the styles in this file apply to your entire
application unless you specify styles at the component level.
styles.css
styles.css
app.component.css
Example Application – todo app
• Creating a new component
• Run the following in CLI:
ng generate component item
item component
• item.component.ts - observe
Add HTML for ItemComponent
• Edit item.component.html
Add HTML for ItemComponent
Add HTML for ItemComponent
• When a user clicks the Edit button, editable becomes true, which
removes this <div> and its children from the DOM.
• If, instead of clicking Edit, a user clicks Delete, the ItemComponent
raises an event that notifies the AppComponent of the deletion.
Add HTML for ItemComponent
Example Application – todo app
• Prepare AppComponent (edit app.component.ts)
import { Item } from "./item";
• Add the following function
Example Application – todo app
• https://www.slideshare.net/sbegaudeau/angular-js-101-everything-yo
u-need-to-know-to-get-started