Tuesday, March 19, 2019

Angular 5: Routing

Routing is installed when you created a new angular project with this parameters:

   ng new ng5 --style=scss --routing

The above will create a file:

   app-routing.module.ts

Inside it modify as follows:

   const routes: Routes = [
  {
    path: '',
    component: HomeComponent
  },
  {
    path: 'about',
    component: AboutComponent
  }
];



Modify your app.component.html as follows:

   <!--The content below is only a placeholder and can be replaced.-->
  <ul>
     <li><a routerLink="">Home</a></li>
     <li><a routerLink="about">About</a></li>
  </ul>

<router-outlet></router-outlet>


app-routing.module.ts

Monday, March 18, 2019

Angular 5: Iterate array and list out elements

To iterate through an array.

In component file:

   goals = [];

In html file:

use ngFor:

<div class="col">
    <p class="life-container" *ngFor="let goal of goals">
      {{ goal }}
    </p>
</div>

Angular 5: Interpolation, Property & Event Binding

Interpolation uses the {{ }} brackets:

export class HomeComponent implements OnInit {
itemCount = 4;
btnText: string = 'Add an item';

constructor() {}

ngOnInit() {}
}

<div class="col">
<p>Add a bucket list item ( {{ itemCount }} )</p>
</div>


<form>
<input type="text" class="txt" name="item" placeholder="life goal.." />
<input type="submit" class="btn" value="{{ btnText }}" />
</form>

Property Binding uses the [ ] brackets:

<form>
<input type="text" class="txt" name="item" placeholder="life goal.." />
<input type="submit" class="btn" [value]="btnText" />
</form>


2-way data binding uses the [(  )]. You need to import the FormsModule in the app.modules.ts file:

import { FormsModule } from '@angular/forms';

@NgModule({
declarations: [AppComponent, HomeComponent, AboutComponent],
imports: [BrowserModule, AppRoutingModule, FormsModule],
providers: [],
bootstrap: [AppComponent]
})

<form>
<input
type="text"
class="txt"
name="item"
placeholder="life goal.."
[(ngModel)]="goalText"
/>
<input type="submit" class="btn" [value]="btnText" />
<br /><span>{{ goalText }}</span>
</form>

Event Binding uses the ( ) brackets:

<input type="submit" class="btn" [value]="btnText" (click)="addItem()" />


A good way to remember all the brackets is as follows:

[    ]    and   {{   }}   is from component to html.

(  )   is from html to component.


[(   )]   is two way between component and html




ok

Sunday, March 17, 2019

Getting Started with Angular 5

Based on:

https://www.udemy.com/angular-5/

https://github.com/paulchin/ng5


Good reference from the horse's mouth:

   https://angular.io/guide/quickstart

To install:

   npm instal -g @angular/cli

To check version after installation:

   ng -v

To the help options:

   ng

To create a new project:

   cd ProjectFolder
   ng new ng5 --style=scss --routing

ng5 is the name of your new app.

To run app:

   cd ng5
   ng serve

To generate a new component:

   ng generate component home
 
You can also use short hand, to generate component:

   ng g c about

An alternative to templateUrl:

@Component({
selector: 'app-home',
template: `
<p>This is my html</p>
`,
styleUrls: ['./home.component.scss']
})

An alternative to styleUrls:

@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styles: [
`
p {
font-weight: bold;
}
div {
color: gray;
}
`
]
})


Enjoy

Which Ionic version is Reo using?

Ionic 3. However latest version is Ionic 4.