Difference between revisions of "Angular"

From RHS Wiki
Jump to navigation Jump to search
Tag: visualeditor
Tag: visualeditor
 
(3 intermediate revisions by the same user not shown)
Line 16: Line 16:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== ng Commands ==
+
==ng Commands==
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
ng new my-app
 
ng new my-app
 
cd my-app
 
cd my-app
 
ng serve
 
ng serve
 +
ng serve --live-reload
 
ng build
 
ng build
 
ng test
 
ng test
Line 28: Line 29:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Tests ==
+
==Tests==
 
ng test runs  Jasmine tests
 
ng test runs  Jasmine tests
  
Line 37: Line 38:
 
ng e2e runs end to end tests with protractor, code at e2e folder
 
ng e2e runs end to end tests with protractor, code at e2e folder
  
== angular.json ==
+
==angular.json==
 
Defines angular commands behavior
 
Defines angular commands behavior
  
== Components ==
+
==Components==
  
  
Line 49: Line 50:
 
Code (TypeScript) -> *.component.ts
 
Code (TypeScript) -> *.component.ts
  
=== app.component.ts ===
+
===app.component.ts===
 
<syntaxhighlight lang="typescript">
 
<syntaxhighlight lang="typescript">
 
import { Component } from '@angular/core';
 
import { Component } from '@angular/core';
Line 64: Line 65:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== app.component.html ===
+
===app.component.html===
 
<syntaxhighlight lang="html">
 
<syntaxhighlight lang="html">
 
<div style="text-align:center">
 
<div style="text-align:center">
Line 76: Line 77:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Syntax ==
+
==Syntax==
  
=== import ===
+
===import===
 
<syntaxhighlight lang="typescript">
 
<syntaxhighlight lang="typescript">
 
import {Component, OnInit} from '@angular/core';
 
import {Component, OnInit} from '@angular/core';
Line 84: Line 85:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== class definition ===
+
===class definition===
 
<syntaxhighlight lang="typescript">
 
<syntaxhighlight lang="typescript">
 
export class WelcomeComponent implements OnInit {
 
export class WelcomeComponent implements OnInit {
Line 98: Line 99:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Login Component example ==
+
==Login Component example==
 
from command line:<syntaxhighlight lang="bash">
 
from command line:<syntaxhighlight lang="bash">
 
ng generate component login
 
ng generate component login
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== login.component.html ===
+
===login.component.html===
 
<syntaxhighlight lang="html">
 
<syntaxhighlight lang="html">
Username: <input type="text" name="username" [(ngModel)]="username">
+
<small *ngIf=invalidLogin>{{ errorMessage }}</small>
Password: <input type="password" name="password" [(ngModel)]="password">
+
<div>
<button (click)=handleLogin()>Login</button>
+
  Username: <input type="text" name="username" [(ngModel)]="username">
 +
  Password: <input type="password" name="password" [(ngModel)]="password">
 +
  <button (click)=handleLogin()>Login</button>
 +
</div>
 +
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== login.component.ts ===
+
===login.component.ts===
 
<syntaxhighlight lang="typescript">
 
<syntaxhighlight lang="typescript">
 
import { Component, OnInit } from '@angular/core';
 
import { Component, OnInit } from '@angular/core';
Line 122: Line 127:
 
   username = 'Rafa'
 
   username = 'Rafa'
 
   password = ''
 
   password = ''
 +
  errorMessage = 'Invalid credentials'
 +
  invalidLogin = false
  
 
   constructor() { }
 
   constructor() { }
Line 129: Line 136:
  
 
   handleLogin(){
 
   handleLogin(){
     console.log(this.username)
+
     // console.log(this.username)
 +
    this.invalidLogin = !(this.username === 'Rafa' && this.password === 'abcd');
 
   }
 
   }
 
}
 
}
 +
  
 
</syntaxhighlight>In order to be able to use [(ngModel)]=... you need to add the formsModule to your app.module.ts:<syntaxhighlight lang="typescript">
 
</syntaxhighlight>In order to be able to use [(ngModel)]=... you need to add the formsModule to your app.module.ts:<syntaxhighlight lang="typescript">
Line 142: Line 151:
 
....
 
....
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
==Routing==
 +
 +
===app-routing.module.ts===
 +
<syntaxhighlight lang="typescript">
 +
import { RouterModule, Routes } from '@angular/router';
 +
import {LoginComponent} from "./login/login.component";
 +
import {ErrorComponent} from "./error/error.component";
 +
 +
const routes: Routes = [
 +
  { path: '', component: LoginComponent},
 +
  { path: 'login', component: LoginComponent},
 +
  { path: 'welcome', component: LoginComponent},
 +
  { path: '**', component: ErrorComponent}
 +
];
 +
 +
@NgModule({
 +
  imports: [RouterModule.forRoot(routes)],
 +
  exports: [RouterModule]
 +
})
 +
export class AppRoutingModule { }
 +
 +
</syntaxhighlight>
 +
 +
=== routing with parameters ===
 +
 +
==== app-routing.module.ts ====
 +
<syntaxhighlight lang="typescript">
 +
import { RouterModule, Routes } from '@angular/router';
 +
import {WelcomeComponent} from "./welcome/welcome.component";
 +
 +
 +
const routes: Routes = [
 +
  { path: 'welcome/:name', component: LoginComponent},
 +
];
 +
 +
@NgModule({
 +
  imports: [RouterModule.forRoot(routes)],
 +
  exports: [RouterModule]
 +
})
 +
export class AppRoutingModule { }
 +
</syntaxhighlight>
 +
 +
==== welcome/welcome.component.ts ====
 +
<syntaxhighlight lang="typescript">
 +
import { ActivatedRoute } from '@angular/router';
 +
import { Component, OnInit } from @angular/core;
 +
 +
@Component({
 +
  selector: 'app-welcome',
 +
  templateUrl: './welcome.component.html',
 +
  styleUrls: ['./welcome.component.css']
 +
})
 +
 +
export class WelcomeComponent implements OnInit {
 +
  message = 'Some Welcome Message'
 +
  name = ''
 +
 
 +
  // ActivatedRoute
 +
  constructor(private route:ActivatedRoute){
 +
  }
 +
 +
  ngOnInit(){
 +
    console.log(this.message)
 +
    name = this.route.snapshot.params['name'];
 +
  }
 +
}
 +
</syntaxhighlight>
 +
 +
==== login/login.component.ts ====
 +
<syntaxhighlight lang="typescript">
 +
import { Component, OnInit } from '@angular/core';
 +
 +
@Component({
 +
  selector: 'app-login',
 +
  templateUrl: './login.component.html',
 +
  styleUrls: ['./login.component.css']
 +
})
 +
export class LoginComponent implements OnInit {
 +
  username = 'Rafa'
 +
  password = ''
 +
  errorMessage = 'Invalid credentials'
 +
  invalidLogin = false
 +
 +
  constructor() { }
 +
 +
  ngOnInit(): void {
 +
  }
 +
 +
  handleLogin(){
 +
    // console.log(this.username)
 +
    this.invalidLogin = !(this.username === 'Rafa' && this.password === 'abcd');
 +
 +
    if ( !this.invalidLogin ) {
 +
      this.router.navigate(['welcome', this.username])
 +
    }
 +
 +
  }
 +
}
 +
</syntaxhighlight>
 +
 +
==Error component==
 +
ng g c error

Latest revision as of 15:37, 10 May 2022

Install Angular requires NodeJS[edit]

npm install -g @angular/cli

# To install specific version
npm install -g @angular/cli@7.0.3

Create and run Angular application[edit]

ng new my-app
cd my-app
ng serve

ng Commands[edit]

ng new my-app
cd my-app
ng serve
ng serve --live-reload
ng build
ng test
ng e2e
ng generate component welcome # = ng g c welcome

Tests[edit]

ng test runs Jasmine tests

Configured at karma.conf.js

Extension for tests: *.spec.ts

ng e2e runs end to end tests with protractor, code at e2e folder

angular.json[edit]

Defines angular commands behavior

Components[edit]

Template -> *.component.html

CSS -> *.component.css

Code (TypeScript) -> *.component.ts

app.component.ts[edit]

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

@Component({
    selector: 'app-root',
    template: './app.component.html',
    styleUrls: ['./app.components.css']
})

export class AppComponent {
    title = 'todo';
}

app.component.html[edit]

<div style="text-align:center">
    <h1>
        Welcome to {{ title }}!
    </h1>
    <img width="300" alt="Angular Logo" src="data:image"
</div>
 
<router-outlet></router-outlet>

Syntax[edit]

import[edit]

import {Component, OnInit} from '@angular/core';
import { AppComponent }from '..app.component';

class definition[edit]

export class WelcomeComponent implements OnInit {
    message = 'Some welcome message'
    message2 : string = 'Some welcome message with type'

    constructor(){
    }
    ngOnInit(){
        console.log(this.message)
    }
}

Login Component example[edit]

from command line:

ng generate component login

login.component.html[edit]

<small *ngIf=invalidLogin>{{ errorMessage }}</small>
<div>
  Username: <input type="text" name="username" [(ngModel)]="username">
  Password: <input type="password" name="password" [(ngModel)]="password">
  <button (click)=handleLogin()>Login</button>
</div>

login.component.ts[edit]

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

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  username = 'Rafa'
  password = ''
  errorMessage = 'Invalid credentials'
  invalidLogin = false

  constructor() { }

  ngOnInit(): void {
  }

  handleLogin(){
    // console.log(this.username)
    this.invalidLogin = !(this.username === 'Rafa' && this.password === 'abcd');
  }
}

In order to be able to use [(ngModel)]=... you need to add the formsModule to your app.module.ts:

....
    imports: [
        BrowserModule,
        AppRoutingModule,
        FormsModule
    ],
....

Routing[edit]

app-routing.module.ts[edit]

import { RouterModule, Routes } from '@angular/router';
import {LoginComponent} from "./login/login.component";
import {ErrorComponent} from "./error/error.component";

const routes: Routes = [
  { path: '', component: LoginComponent},
  { path: 'login', component: LoginComponent},
  { path: 'welcome', component: LoginComponent},
  { path: '**', component: ErrorComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

routing with parameters[edit]

app-routing.module.ts[edit]

import { RouterModule, Routes } from '@angular/router';
import {WelcomeComponent} from "./welcome/welcome.component";


const routes: Routes = [
  { path: 'welcome/:name', component: LoginComponent},
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

welcome/welcome.component.ts[edit]

import { ActivatedRoute } from '@angular/router';
import { Component, OnInit } from @angular/core;

@Component({
  selector: 'app-welcome',
  templateUrl: './welcome.component.html',
  styleUrls: ['./welcome.component.css']
})

export class WelcomeComponent implements OnInit {
  message = 'Some Welcome Message'
  name = ''
  
  // ActivatedRoute
  constructor(private route:ActivatedRoute){
  }

  ngOnInit(){
    console.log(this.message)
    name = this.route.snapshot.params['name'];
  }
}

login/login.component.ts[edit]

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

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  username = 'Rafa'
  password = ''
  errorMessage = 'Invalid credentials'
  invalidLogin = false

  constructor() { }

  ngOnInit(): void {
  }

  handleLogin(){
    // console.log(this.username)
    this.invalidLogin = !(this.username === 'Rafa' && this.password === 'abcd');

    if ( !this.invalidLogin ) {
      this.router.navigate(['welcome', this.username])
    }

  }
}

Error component[edit]

ng g c error