Wednesday, January 25, 2017

4-DataBinding btw Component(.ts)/view(.html) or Components

String interpolation :
{{title}} - always resolves to a string
ie, using double {{ }} will redirect to the value in the.ts file for the enclosed string, always referring to a string value.


Property binding:
Ex1:<input [value] = "functionName"> - always resolves to a variable or a function with name functionName, defined in the component class as shown below.
Ex2:<input required = true> - resolves to forcing the user to enter input in the input field, Just as a "required=true property in javascript.
Ex3:<input [required] = true> - this does nothing.

Example of "functionName " defined in a the relevant(The.ts file defining the component class related to the html in which this tag is embedded) component class definition is as follows:

Event binding:
<button<<eventtype(ex.click)>> = 'expression/function'> - events triggers on a element such as button results in an expression or triggers a function.
 -Component class can handle the event.
ex: <button (click)="alertme('heyyyy')" >Click me </button>
So, in the above case, a method called alertme, which takes in a parameter, 'heyyyy', is called from within the Component class.

____________________________________________________________
two-way data binding: 
1)data passed from/to Component(.ts) <==> View(.html)
using ngModel tag as in the example below to directly assign the value entered within the input tag to model or object as in the statement below.
<input [(ngModel)] = "model/object"> - Updates the model or object class in the component class

{{model}}

Objects in Typescript are created in similar syntax as in javascript.
>> Create a Object as below in Component class(.ts file)

  • Component(.ts) --> View(.html)ie, view gets updated based on object defined in component class >> Now, access this object property in the related view/html file as below
  • Component(.ts) <-- View(.html)ie, viewcomponent gets updated based on object modified in view via <input [(ngModel)]=""> tag >>Note that, as and when we update the input field, the value of the variable also gets updated,,, hence displays all the changes done in the <input tag.(refer the browser in the above figure)


2)data passed from Root==>Home
(@Input() directive)
Done in 4 steps(refer screenshot below:)
  • (step1Define the object/variable in the "Root" Component class(<<root>>.ts), that which you intend to pass to the "Home" component.  
    • rootPerson = { rootPersonName: "rootdidi", rootPersonAge: "20"}
  • (step2define the RootComponent's Object/variable within the home-component's selector tag(<app-home></app-home>)used in the RootView(.html of root)
    • <app-home [rootPersonVariable] = "rootPerson"></app-home>
  • (step3) declare and import the @Input() directive within the Home component as below,
    • @Input() rootPersonVariable;
  • (step4) use the Root Variable like anyother HomeComponent variable as in code below:
    • <p>access Root Person :
    •     <label>{{rootPersonVariable.rootPersonName}}</label>
    •  </p>
          Now, access the url(http://localhost:4200) to see the Root Object value displayed:


     


3)data passed from Home==>Root(or say Custom Event Binding)
(@Output() directive) 
Done in 4 steps(refer screenshot below:)
  • (step1Define the function in "Root" Component(.ts), that needs to get triggered on eventExecuted over a "Home" Component Tag(<app-home>) on Root View.  
    • yell(){
    •     alert("I am yelling from yell() of Root Component");
    •   }
  • (step2Declare the call to the Root event function(yell()) of the "Root" Component(.ts), within the"Home" Component Tag(<app-home>) on Root View as below: 
    • <app-home [rootPersonVariable]="rootPerson"
    •   (onYellHomeEventVariable)="yell($event)"></app-home>
  • (step3) Declare the above variable(onYellHomeEventVariable) using the @Output directive(also remember to import this in the .ts file) in the Home Component(.ts), as below:
    • @Output  onYellHomeEventVariable= new EventEmitter();
    • onYellEvent(e){
    •   this.onYellHomeEventVariable.emit(e);
    • }
  • (step4Define the above custom event(onYellEvent(e) on Home View's(.html) button click event, as below:
    • <button (click)="onYellEvent($event)">click me</button>
Now, access the url(http://localhost:4200) and click on "Click me" button to see the Button event of Root fired while the event declared within Home tag<app-home>

___________________________________________________________________________

3-Components/sub-components/ng-content

Components:
(i)Root component 
 In reference to the automated template Angular project(refer 1st link), The project already has a Root component generated by default. The Root component related files(listed below) gets generated in the location, c: users\someone\project-location\projectname>


(ii)sub-components
>>access the terminal frmo within the atom editor to generate sub components for the Angular app by navigating to "Packages>>platform-ide-terminal>>New Terminal".
>>This will open up a terminal for the relative Angular app with the location :
c: users\someone\project-location\projectname>.
>>Enter the below commands to naviagate to the src/app folder of the project,
c: users\someone\project-location\projectname> cd .\src\app
>>Then, Execute the below command to generate sub Components under the Root Component location: c: users\someone\project-location\projectname\src\app> ng generate component <<component-name>>
note: 


Lets call the sub component as "home"

Hence now, the below files are created for the "home" component under the Root component "app", as below:

  • the "apps.modules.ts" file updates the new component created
  • the ---.ts file defines the required component definition along with the relative properties as in the content of the .ts file content for the created sub-component below:
import { Component, OnInit } from '@angular/core';
    @Component
    ({
     selector: 'app-home',
     templateUrl: './home.component.html',
     styleUrls: ['./home.component.css']
    })
    export class HomeComponent implements OnInit 
    {
    titleStrFromHomeComponent ="Demo of String interpolation";
    constructor() { }
        ngOnInit() 
        {
        }
      }
      //Implement OnInit triggers the ngOnInit() method first to initialiaze any variables within this //method when this component(Home) is called. also, notie that the import also imports the //implemented class OnInit


      Embedding the sub-component in the Root component view:

      • Every Component file(.ts) file has a directive, @Component defined.
      • every Component directive has a set of properties as below, which configures a component with related view(.html), style(.css), tag(used in html files):
        • selector: 'app-home',
        • templateUrl: './home.component.html',
        • styleUrls: ['./home.component.css']
      • selector property specifies the tag value used as below in the view(.html files)


      _______________________________________________________________________

      Now, refresh the localhost link to see the results: http://localhost:4200


      _______________________________________________________________________

      <ng-content></ng-content> in .html files of the related component, enables the functioning of content that is added in the <selector tag of the component>,
      If this tag is not inlcuded, then anything that is added within the component selector tag of the Root html file will be disabled. See the example below when
      (1)<ng-content></ng-content>  used
      (2)<ng-content></ng-content>  not used

      (1)<ng-content></ng-content>  used



      (2)<ng-content></ng-content>  not used

      _______________________________________________________________________


      2-Typescript - Language used in Angular 2JS

      TypeScript: Angular2JS is written in Typescript. Its a superset of javascript. It has a few additional rules as below:

      1)myVar = "astring"; 
      myVar = 23; (now assigned to an integer) allowed in javascript.
      However, this isnt allowed in Typescript to change the type of the variable.

      2)Unlike in javascript, in Typescript, we could declare the type o the variable as below. Its optional
      Ex: Types are :
      • string.Ex: myVar:string="hello" (or) myVar="hello"
      • numberEx: myVar:number=21 (or) myVar=21
      • booleanEx: myVar:boolean=true (or) myVar=true
      • arraysEx: myVar:number[]
      • any type as inEx: myVar: any ; myVar=50.
      • classesEx: myVar:<<class-name>>=new <<class-name>>()// as in java
        • Ex: Class definition: 
                              class Car            
                              {
                  
                               speed:number; 
                                          constructor(mph:number)
                                          {
                                                      this.speed = mph;
                                          }
                              }
      • Class instance: 
                              myCar:Car = new Car(70);// instance creation using number constructor

      --------------------------------------------------------------------------------------------------------------

      1-Introduction -installations/setUp


      Angular 2 is one of the most popular JavaScript frameworks for creating dynamic web applications.

      Requisites:

      1)Node Package Manager(*Download Node.js - https://nodejs.org/en/) ---now, Jan2017 - Node(6.9.4). This package is needed in order to install Angular CLI

      2)Atom editor - https://atom.io/... Once installed.. Open the application adn also install the below components from its packages tab:

      • platformio-ide-terminal
      • language-typescript-grammers-only


      Core features of Angular2:
      1) Components
      2) Directives
      3) Templates


      Now, once 1) and 2) are installed, Open command prompt to install Angular CLI using the Node Package Manager. With the following commands:


      Steps to create and set up First Template project:
      c: users\someone> node -v  (version was 6.9.4 when installed)
      c: users\someone> npm install -g angular-cli  (wait for the installation....)

      >>Create a location for your angular project.
      >>Open that location on ATOM editor(which is currently blank)
      >>Now enter the below command on your command prompt to generate a general ANGULAR project,
      c: users\someone\project-location>ng new project-name 
      >>If you now look at the atom editor, U'll see the newly created project with its generated required files and folder structure.
      >>Now, Browse into the project directory via cmd prompt use Angular2 CLI to generate and deploy the project to a server using the below command
      c: users\someone\project-location\projectname>ng serve

      >>This create and deploys and returns a url for the localhost server such as http://localhost:4200

      >>Now, access this link to see the successful deployment of our first angular JS app. If the "

      app works!

      " message appears on the webpage, Then its a successful first angular app deployment on localhost server

      --------------------------------------------------------------------------------------------------------------






      References : https://www.youtube.com/watch?v=DwTNR3EBSJQ&index=1&list=PL4cUxeGkcC9jqhk5RvBiEwHMKSUXPyng0
      Angular2 tutorials by The Ninja

      Updated better Reference: https://www.youtube.com/watch?v=DBjPIabiRNg