Cosmi

Personnel Reference Blog

Wednesday, January 10, 2018

Accelerating Through Angular
















































































_________________________________________________________________________________


npm WARN saveError ENOENT: no such file or directory, open '/Users/cosmi/Documents/angular_April14/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/Users/cosmi/Documents/angular_April14/package.json'
npm WARN angular_April14 No description
npm WARN angular_April14 No repository field.
npm WARN angular_April14 No README data

npm WARN angular_April14 No license field.
References


  • https://medium.baqend.com/angular-2-by-example-e85a09fa6480

Angularjs vs Angular

  1. SPEED - faster
  2. COMPONENTS instead of controllers and scopes
  3. SIMPLER DIRECTIVES
  4. INTUTIVE DATA BINDING 
  5. SIMPLER SERVICES


WAYS TO WRITE

  1. TYPESCRIPT > JS
  2. BABEL >JS
http://typesrcriptlang.org
http://go.codeschool.com/angularstart























COMPONENT IS THE BASIC BUILDING BLOCK OF ANGULAR APPLICATIONS

@import {Component} from '@angular/core';


class {Component} from '@angular/core'



In order to run application we need root module

import{NgModule, Component} from '@angular/core';

@Component({
selector: 'my-app',
template:'Ultra Racing'

})
class Component {
}


The over all flow




Each component can consist of :
  1. class file
  2. html file
  3. css file

NOTE: IN TYPESCRIPT

class Component {
title ='Ultra Racing' 
}

Inside a typescript class , we dont use the var or let keywords to declare class properties

@Component({
selector: 'my-app',
template:'

{{

title}}
'})
class Component {
title =Ultra Racing 
}

Accessing objects inside component 


  1. Level 2
    Template Traction
    • Structural Directives
      1.  2.1 Structural Directives
      2.  2.2 Building Blocks
      3.  2.3 Differences
      4.  2.4 Looping
      5.  2.5 Conditionals
      6.  2.6 Directive Types
    • Pipes & Component Methods
      1.  2.7 Pipes & Component Methods
      2.  2.8 Pipes
      3.  2.9 Methods

*ngIf  & *ngFOR

class Component{]

totalCost() { let sum = 0; for (let race of this.races){ if(race.isRacing) { sum += race.entryFee; } } return sum; }
__________________________________________________________________________________






















__________________________________________________________________________________


STEP 1: IMPORT THE CLASSES THAT YOU WISH TO USE:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { RacesComponent } from './races.component';

STEP 2: DEFINE NG-MODULE:
@NgModule({
  imports: [ BrowserModule ],
  declarations: [ AppComponent,RacesComponent ],
  bootstrap: [ AppComponent ]
})
class AppModule {}

platformBrowserDynamic().bootstrapModule(AppModule);
------------------------------------------------------------------------------------


import { Component } from '@angular/core';

@Component({
  selector: 'my-races',//the directive name with which it will be included in html
  template: `
  Cash left to enter races: {{cashLeft() |
currency:'USD':true}}  











  • *ngFor="let race of races">

  •    {{race.name}} {{race.entryFee |
    currency:'USD':true}}
            {{race.date | date:'MMM d, y, h:MM a'}}
            {{race.about}}
           
           

    *ngIf="race.isRacing">Already Racing

    Total cost: {{totalCost() | currency:'USD':true}}`})

    export class RacesComponent {
      heading = "Ultra Racing Schedule"
      cash = 10000;
      races = [{
        "id": 1,
        "name": "Daytona Thunderdome",
        "date": new Date('2512-01-04T14:00:00'),
        "about": "Race through the ruins of an ancient Florida battle arena.",
        "entryFee": 3200,
        "isRacing": false
      }, {
        "id": 2,
        "name": "San Francisco Ruins",
        "date": new Date('2512-07-03T20:00:00'),
        "about": "Drift down the streets of a city almost sunk under the ocean.",
        "entryFee": 4700,
        "isRacing": true
      }, {
        "id": 3,
        "name": "New York City Skyline",
        "date": new Date('2512-07-12T21:00:00'),
        "about": "Fly between buildings in the electronic sky.",
        "entryFee": 4300,
        "isRacing": true
      }];

      totalCost() {
        let sum = 0;
        for (let race of this.races) {
          if (race.isRacing) sum += race.entryFee;
        }
        return sum;
      }

      cashLeft() {
        return this.cash - this.totalCost();
      }

    } 
    ---------------------------------------------------------------------------------------
    import { Component } from '@angular/core';

    @Component({
      selector: 'racing-app',
      template: `
       

    {{heading}}

            my-races
      `
    })
    export class AppComponent {
      heading = "Ultra Racing Schedule"

    }
    ____________________________________________________________________________________


























    app.component.races.ts



    import { Component } from '@angular/core';



    How to  



    @Component({

      selector: 'my-races',

      templateUrl:'app/races.component.html',

      styleUrls['app/races.component.css'];

    })
    export class RacesComponent {
      heading = "Ultra Racing Schedule"
      cash = 10000;
      races = [{
        "id": 1,
        "name": "Daytona Thunderdome",
        "date": new Date('2512-01-04T14:00:00'),
        "about": "Race through the ruins of an ancient Florida battle arena.",
        "entryFee": 3200,
        "isRacing": false
      }, {
        "id": 2,
        "name": "San Francisco Ruins",
        "date": new Date('2512-07-03T20:00:00'),
        "about": "Drift down the streets of a city almost sunk under the ocean.",
        "entryFee": 4700,
        "isRacing": true
      }, {
        "id": 3,
        "name": "New York City Skyline",
        "date": new Date('2512-07-12T21:00:00'),
        "about": "Fly between buildings in the electronic sky.",
        "entryFee": 4300,
        "isRacing": true
      }];

      totalCost() {
        let sum = 0;
        for (let race of this.races) {
          if (race.isRacing) sum += race.entryFee;
        }
        return sum;
      }

      cashLeft() {
        return this.cash - this.totalCost();
      }

    }


    app.component.races.html



    Cash left to enter races: {{cashLeft() | currency:'USD':true}}



       










    •    

      {{race.name}} {{race.entryFee | currency:'USD':true}}


          {{race.date | date:'MMM d, y, h:MM a'}}

          {{race.about}}

         

         

      Already Racing


       

      Total cost: {{totalCost() | currency:'USD':true}}



      app.component.races.css


      h2 {

        color: green;

      }

      p {

        font-size: small;

      }

      ____________________________________________________________________________________

      MOCKS AND MODELS


      1. Level 3
        Tuning It Up
        • Splitting to Two Components
          1.  3.1 Splitting to Two Components
          2.  3.2 Including Classes
          3.  3.3 Refactoring Classes
        • Component HTML & CSS
          1.  3.4 Component HTML & CSS
          2.  3.5 Adding HTML and CSS
        • Mocks & Models
          1.  3.6 Mocks & Models
          2.  3.7 Data Organization
          3.  3.8 Modeling and Mocking

























      ______________________________________________________________________________
      Step1  Define  mock defintion: race.ts

      export class Race {
        id: number;
        name: string;
        date:  Date;
        about: string;
        entryFee: number;
        isRacing: boolean;
      }

      ______________________________________________________________________________
      Step2 : Move data to mock.ts

      import { Race } from './race';

      export const RACES: races[]=[{
        "id": 1,
        "name": "Daytona Thunderdome",
        "date": new Date('2512-01-04T14:00:00'),
        "about": "Race through the ruins of an ancient Florida battle arena.",
        "entryFee": 3200,
        "isRacing": false
      }, {
        "id": 2,
        "name": "San Francisco Ruins",
        "date": new Date('2512-07-03T20:00:00'),
        "about": "Drift down the streets of a city almost sunk under the ocean.",
        "entryFee": 4700,
        "isRacing": true
      }, {
        "id": 3,
        "name": "New York City Skyline",
        "date": new Date('2512-07-12T21:00:00'),
        "about": "Fly between buildings in the electronic sky.",
        "entryFee": 4300,
        "isRacing": true

      }];
      ______________________________________________________________________________

      @Component({
        selector: 'my-races',
        templateUrl: 'app/races.component.html',
        styleUrls:['app/races.component.css']
      })
      export class RacesComponent {
        heading = "Ultra Racing Schedule"
        cash = 10000;
        races: Race[];
        ngOnInit(){
          this.races = RACES;
        }

        totalCost() {
          let sum = 0;
          for (let race of this.races) {
            if (race.isRacing) sum += race.entryFee;
          }
          return sum;
        }

        cashLeft() {
          return this.cash - this.totalCost();
        }



      }


      ______________________________________________________________________________

      TEMPLATE

      Cash left to enter races: {{cashLeft() | currency:'USD':true}}


         






      • *ngFor 
      • ="let race of races">   

        {{race.name}} {{race.entryFee | currency:'USD':true}}

            {{race.date | date:'MMM d, y, h:MM a'}}
            {{race.about}}
           
        ="!race.isRacing">Enter Race   

        *ngIf

        ="race.isRacing">Already Racing 


        Total cost: {{totalCost() | currency:'USD':true}}


        ______________________________________________________________________________




        ______________________________________________________________________________
        CSS FILE

        h2 {
          color: green;
        }
        p {
          font-size: small;

        }

        ______________________________________________________________________________

        Step3:Convert mock.ts on to 


        Lastly, we need to set the races in our RacesComponent equal to the RACESmock. You know, inside the ngOnInit().

        _____________________________________________________________________________

        1. Level 4
          Data Binding Boost
          • Property & Class Binding
            1.  4.1 Property & Class Binding
            2.  4.2 Binding to What?
            3.  4.3 Template Property Binding
          • Event Binding
            1.  4.4 Event Binding
            2.  4.5 Racing
            3.  4.6 Sending Event Data
          • Two-way Binding
            1.  4.7 Two-way Binding
            2.  4.8 Input Binding
            3.  4.9 ngModel



        What is property binding ?

        • Property binding allows us to bind component properties to any DOM element properties.
        • Any update to the component property value will update the DOM property ,but not vice versa - that's when it's one-way binding 
        • Class binding allows us to specify  a CSS class to add to DOM element if a component property is true






























        Adding image to template




        secret
        How to add featured class?

        export let CARPARTS: CarPart[]=[{
        id:1,
        "featured":false

        },{
        id:1,
        "featured":true

        },{
        id:1,
        "featured":false

        }];

        export class CarPart{
         image :string 
          featured:boolean
        }

        ________________________________________________________________________________

        race.component.html



        Cash left to enter races: {{cashLeft() | currency:'USD':true}}

















        When  you run an Angular application ,it creates a dependency injector.An injector is in charge of knowing how to create and send things.

        Service fuel injection



















































        Race
































        1. Level 5
          Service Fuel Injection
          • Services
            1.  5.1 Services
            2.  5.2 Injector
            3.  5.3 Moving to a Service
          • Http
            1.  5.4 Http
            2.  5.5 Over the Internet
        ________________________________________________________________________________

        • app/race.service.ts

          import { RACES } from './mocks';
          import { Injectable } from '@angular/core';
          
          @Injectable()
          export class RaceService {
            getRaces() {
              return RACES;
            }
          }
          
        • app/main.ts

          import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
          import { BrowserModule } from '@angular/platform-browser';
          import { NgModule } from '@angular/core';
          import { FormsModule } from '@angular/forms';
          import { AppComponent } from './app.component';
          import { RacesComponent } from './races.component';
          import { RaceService } from './race.service';
          
          @NgModule({
            imports: [ BrowserModule, FormsModule ],
            declarations: [ AppComponent ],
            providers: [ RaceService ],
            bootstrap: [ AppComponent ]
          })
          class AppModule {}
          
          platformBrowserDynamic().bootstrapModule(AppModule);
          
        • app/races.component.ts

          import { Component } from '@angular/core';
          import { Race } from './race';
          import { RaceService } from './race.service';
          
          @Component({
            selector: 'my-races',
            templateUrl: 'app/races.component.html',
            styleUrls:['app/races.component.css']
          })
          export class RacesComponent {
            heading = "Ultra Racing Schedule"
            cash = 10000;
            races: Race[];
          
            constructor(private raceService: RaceService) { }
          
            ngOnInit() {
              this.races = this.raceService.getRaces();
            }
          
            totalCost() {
              let sum = 0;
              for (let race of this.races) {
                if (race.isRacing) sum += race.entryFee;
              }
              return sum;
            }
          
            cashLeft() {
              return this.cash - this.totalCost();
            }
          
            enterRace(race) {
              if (this.cashLeft() > race.entryFee) {
                race.isRacing = true;
              } else {
                alert("You don't have enough cash");
              }
            }
          
            cancelRace(race) {
              race.isRacing = false;
            }
          
          }



        ________________________________________________________________________________
        HTTP












        ________________________________________________________________________________




























        References

        ANGULAR



        REST API USNING ANGULAR

        https://dzone.com/articles/angular-js-use-angular



        https://examples.javacodegeeks.com/core-java/real-time-applications-angularjs-java-part-3/



        http://spring.io/guides/gs/consuming-rest-angularjs/



        http://www.baeldung.com/angular-js-rest-api




        https://howtodoinjava.com/angularjs/angularjs-http-restful-api-example/


        https://medium.com/craft-academy/connecting-an-api-to-an-angular-4-front-end-application-e0fc9ea33202

        at January 10, 2018
        Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
        Labels: Angular, JavaScript

        No comments:

        Post a Comment

        Newer Post Older Post Home
        Subscribe to: Post Comments (Atom)
        • Java 8 : Streams at Go:
          JAVA 8                          STREAM INTSTREAM LONGSTREAM DOUBLESTREAM OF Stream. Of (array) Stream. of ( "str1" , "st...
        • Devtools
          Chrome updates https://developers.google.com/web/updates/2018/03/nic65 Developer tools allows you to  https://developers.google.com/w...

        Search This Blog

        • Home

        About Me

        My photo
        Cosmi
        Full Stack Developer
        View my complete profile

        Categories

        • Algorithms (27)
        • JavaScript (18)
        • Java 8 (16)
        • Random Thoughts (15)
        • User Interface (10)
        • interview (10)
        • Java (7)
        • Spring 4 (5)
        • Angular (4)
        • Dynamic Programming (4)
        • Machine Learning (4)
        • Cache (3)
        • ArcGIS (2)
        • Persistence (2)
        • Problems (2)
        • empty (2)
        • AWS (1)
        • Analytics (1)
        • AngularJS (1)
        • CSS (1)
        • Charts (1)
        • Concurrency (1)
        • Connection Pooling (1)
        • DOCKER (1)
        • Data (1)
        • Design Patterns (1)
        • GIS (1)
        • GIT (1)
        • Good (1)
        • Google APIs (1)
        • GraphQL (1)
        • HashMap (1)
        • Hibernate (1)
        • Issues (1)
        • JS (1)
        • K8 (1)
        • LINKS (1)
        • Linux (1)
        • Others (1)
        • Python (1)
        • Search Engine (1)
        • Security (1)
        • Spring BOOT (1)
        • Testing (1)
        • To do (1)
        • UML (1)
        • Web Services (1)
        • django (1)
        • eclipse (1)
        • facebook (1)

        Blog Archive

        • ►  2023 (1)
          • ►  November (1)
        • ►  2020 (2)
          • ►  July (1)
          • ►  February (1)
        • ►  2019 (4)
          • ►  December (1)
          • ►  November (1)
          • ►  May (1)
          • ►  April (1)
        • ▼  2018 (69)
          • ►  September (1)
          • ►  July (2)
          • ►  June (3)
          • ►  May (1)
          • ►  April (5)
          • ►  March (15)
          • ►  February (5)
          • ▼  January (37)
            • Spring BOOT
            • Graph Algorithms:
            • Greedy Algortihms
            • Searching & Sorting Problems
            • Algos: Qestions
            • Linkedlist :Questions
            • Bit Algorithms :Questions
            • Arrays
            • List based Problems
            • Algorithms : A birds eye view
            • DP :Rat maze, Minimum cost path
            • Arrays: quicksort type problems
            • Arrays: subarray problems
            • Machine Learning : Python
            • Graph Algorithms
            • CACHE EVICTION ALGORITHMS : BEST PRACTICES
            • SQL
            • Google Maps
            • Substring search/matching algorithms: KMP, Rabin-...
            • Accelerating Through Angular
            • CI/CD PIPELINE : JENKINS
            • Algorithms: Pattern matching
            • Subsequence Problems
            • Recursion And Back tracking
            • Algorthims : Divide and Conquer
            • Algorithms : Greedy Algorithms
            • Dynamic Programming
            • Understanding Data Science
            • DOCKER - SERIES 1
            • JS Concepts : Prototypes
            • Data Structures
            • Bit Manipulation Algorithms
            • Concurrency
            • Security Algorithms
            • Algorithms
            • Java 8 : Streams at Go:
            • Java 8 Collectors at a Go
        • ►  2017 (99)
          • ►  December (99)
        • ►  2015 (4)
          • ►  September (2)
          • ►  August (1)
          • ►  July (1)
        • ►  2014 (1)
          • ►  August (1)
        • ►  2013 (2)
          • ►  May (1)
          • ►  March (1)
        • ►  2010 (1)
          • ►  February (1)
        • ►  2009 (5)
          • ►  December (1)
          • ►  September (1)
          • ►  May (1)
          • ►  April (2)

        Report Abuse

        Subscribe To

        Posts
        Atom
        Posts
        Comments
        Atom
        Comments

        visitors

        My Shelfari

        Shelfari: Book reviews on your book blog

        Translate

        Pageviews

        17544

        Subscribe To

        Posts
        Atom
        Posts
        Comments
        Atom
        Comments

        Friends

        Simple theme. Theme images by RBFried. Powered by Blogger.