fabiobiondi

esempio per avanade

Feb 14th, 2022 (edited)
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // app.component
  2. import { Component, OnInit } from '@angular/core';
  3. import { HttpClient } from '@angular/common/http';
  4. import { User } from '../../model/user';
  5. import { NgForm } from '@angular/forms';
  6.  
  7. @Component({
  8.   selector: 'ava-catalog',
  9.   template: `
  10.     <form #f="ngForm" (submit)="addUser(f)">
  11.       <input type="text" ngModel name="name" required>
  12.       <input type="text" ngModel name="phone">
  13.       <button type="submit" [disabled]="f.invalid">ADD</button>
  14.     </form>
  15.    
  16.     <li *ngFor="let user of users">{{user.name}}</li>
  17.   `,
  18. })
  19. export class CatalogComponent {
  20.   users: User[] = [];
  21.  
  22.   constructor(private http: HttpClient) {
  23.     http.get<User[]>('https://jsonplaceholder.typicode.com/users')
  24.       .subscribe(res => {
  25.         this.users = res;
  26.       })
  27.   }
  28.  
  29.   addUser(form: NgForm) {
  30.     console.log(form.value)
  31.     this.users.push(form.value)
  32.   }
  33.  
  34. }
  35.  
  36.  
  37. // model/user.ts
  38.  
  39. export interface Geo {
  40.   lat: string;
  41.   lng: string;
  42. }
  43.  
  44. export interface Address {
  45.   street: string;
  46.   suite: string;
  47.   city: string;
  48.   zipcode: string;
  49.   geo: Geo;
  50. }
  51.  
  52. export interface Company {
  53.   name: string;
  54.   catchPhrase: string;
  55.   bs: string;
  56. }
  57.  
  58. export interface User {
  59.   id: number;
  60.   name: string;
  61.   username: string;
  62.   email: string;
  63.   address: Address;
  64.   phone: string;
  65.   website: string;
  66.   company: Company;
  67. }
  68.  
  69.  
  70.  
Add Comment
Please, Sign In to add comment