Giter Site home page Giter Site logo

angular-crash's Introduction

AngularCrash

This is a crash course/example angular app

  • Done 6-21 ish Angular Contract :) coming my way
  • Jumping from the React world to Angular

Notes: Angular Crash Course

  • When do I want to build my own modules & how do I do so?
  • Application state ? Do I use redux or does it use its own

Add npm package:

  • ng add @fortawesome/angular-fontawesome
  • Note: you can do npm install as well, however NG add will install multi packages if they are needed for component you are installing

Http: (HttpClient, HttpHeaders)

  • Note: Angular has its own http library it uses

  • Steps

    • add import to component class that will make http requests/calls
      import {HttpClient, HttpHeaders} from '@angular/common/http'
    
    • In app.module add import & import to ngModule
      import { HttpClientModule } from '@angular/common/http';
    
      @NgModule({imports: [HttpClientModule]})
    
    • In constructor of service class add params to constructor parameter declaration
        constructor(private http: httpClient) {}
    

    Examples of Http methods in service class

    getTasks(): Observable<Task[]> {
      return this.http.get<Task[]>(this.apiUrl);
    }
    
    deleteTask(task: Task): Observable<Task> {
      const url = `${this.apiUrl}/${task.id}`;
      return this.http.delete<Task>(url);
    }
    

    Examples in Component Class calling service methods using HTTP

    ngOnInit(): void {
      this.taskService.getTasks().subscribe((tasks) => (this.tasks = tasks));
    }
    
    deleteTask(task: Task) {
      this.taskService
        .deleteTask(task)
        .subscribe(
          () => (this.tasks = this.tasks.filter((t) => t.id != task.id))
        );
    }
    

Observables:

  • In the service
    •   import { Observable, of } from 'rxjs';
      
    • Add class method
        getTasks(): Observable<Task[]> {
          const tasks = of(TASKS);
          return tasks;
        }
      
  • In the component consuming the observable
    • Add import to component that is using service (import service)
    • Add this to ngOnInit()
     ngOnInit(): void {
      this.taskService.getTasks().subscribe((tasks) => (this.tasks = tasks));
    }
    
    Create Service:
  • ng generate service services/task
  • Add method to class
  • Add import to component that is using service
  • Add params to class constructor
  • If adding to init(quick a dirty method)
      Add service method call in ngOnInit()
      ngOnInit(): void {
        this.tasks = this.taskService.getTasks();
      }
    
  • With Observable method
    • Add service method call
      getTasks(): Observable<Task[]> {
        const tasks = of(TASKS);
        return tasks;
      }
      

Create a Module:

Create a Component:

 ng generate component <folder/file>

Create event for component

  • In component html, on the html element add

      (click)="onClick()"
    
  • In component class file add

      function onClick() {}
    
  • Add Output & EventEmitter Class

    • to imports
  • Add to Class

      @Output <btnClick> = new EventEmitter();
    
    • In function add
        this.btnClick.emit();
      
    • Note: This just adds an emitter to our function
  • Navigate to the components html that is rendering our component above

    Add (btnClick)="toggleAddTask()"
    
  • To the component

  • Navigate to component class that is rendering this component

    • Add function in class for component
        toggleAddTask() {}
      

angular-crash's People

Contributors

ssmith777 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.