Difference between revisions of "Angular"

From RHS Wiki
Jump to navigation Jump to search
Tag: visualeditor
Tag: visualeditor
 
Line 173: Line 173:
 
export class AppRoutingModule { }
 
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>
 
</syntaxhighlight>
  
 
==Error component==
 
==Error component==
 
ng g c error
 
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