Skip to content

Angular

Kota edited this page Oct 24, 2018 · 28 revisions
  • https://angular.io

  • Application = Components + Services | Components = View + Class + MetaData

  • Modules => Root Angular Module (components) + Feature Angular Modules (components)

  • Supports ES 5. Scripts written in ES 2015 (ES 6) or Typescript should be transpiled. ES = ECMA Script (European Computer Manufacturer's Association)

  • Angular is modular (consists of many modules) https://www.npmjs.com/~angular

  • Why not Javascript

    • Doesn't provide namespace and all methods/function will be under global namespace
    • No Code Organization
    • AngularJS Modules helps in addressing it (In ES 2015, a file is a module).
      • ES 2015 module
      // exporting module 
      export class Product { }     -> transpiles to ->   function Product() { }
      
      // importing it for use
      import { Product } from './product'
      
      • Angular Module: can communicate to other modules and a module can have many components

component

  • Includes a template (view) which is created with HTML. includes binding and directives to show the intended View
  • Contains a Class (Code, ex: typescript) with Properties and Methods
  • Contains Metadata, which is defined with a decorator
  • Component = Template + Class (Properties,methods) + Metadata
import { Component } from '@angular/core';

// Metadata & Template  : Like an attribute for the class
@Component ({      
  selector: 'app-root',   // directive name - a custom element
  template: `<div> <h1> {{ pageTitle }} </h1> 
                <div> My First Component </div>
             </div>`
})
export class AppComponent {                     // Class
  pageTitle: string = 'Hello World!'
}

// index.html
<body>
  <pm-root></pm-root>
</body>

// module
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent} from './app.component';

@NgModule({
  imports: [ BrowserModule ],         // external modules
  declarations: [ AppComponent ],     // our custom components
  bootstrap: [ AppComponent ]         // startup components
})
export class AppModule {}
  • Decorator adds metadata to a class, its members, or its method arguments (prefixed with @) (~ attribute)
  • Module organizes and loads the required components and bootstraps what is required
  • Component should belong to only one module
  • Any new component added should be loaded in the base module so that angular can find it.
  • template: use "" for inline template. backticks (`) for multiple lines. Or a templateUrl: './test.html'
  • Directives: Custom & Angular Built In (Ex: *ngIf, *ngFor - structural directives)
  • Interpolation:
    • {{ }} One way Binding
    • <img [src]='product.imageUrl' /> property binding
    • <img src={{product.imageUrl}} /> property binding
    • <button (click)='toggleImage()' /> event binding
    • <input [(ngModel)]='listFilter' > two way binding

Clone this wiki locally