Advertisement
fenisoft

Untitled

Dec 28th, 2019
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, Input ,ViewChild,ElementRef,EventEmitter,Output} from '@angular/core';
  2. import { fromEvent } from 'rxjs';
  3. import { map, filter } from 'rxjs/operators'
  4.  
  5. @Component({
  6.   selector: 'app-root',
  7.   template:`
  8.      <app-child [request]="request" (change-value)="handleChange($event)"></app-child>`
  9. })
  10. export class AppComponent {
  11.    public request = {
  12.      waypoints : [ {location: 'Pisa'} , {location:'Pistoia'}],
  13.      avoidToll: false
  14.    }
  15.    handleChange(r) {
  16.      this.request =  r;
  17.      console.log(this.request);
  18.    }
  19. }
  20.  
  21. @Component({
  22.   selector: 'app-child',
  23.   template: `
  24.       <!!*=.=>
  25.          <app-input-element [value]="item.location" [index]="i" (change-value)="handleChange($event)"></app-input-element>
  26.       </div>`
  27. })
  28. export class ChildComponent{
  29.   @Input('request') request:any;
  30.   @Output('change-value') changeValue = new EventEmitter<any>();
  31.  
  32.   handleChange(value:any){
  33.     /*
  34.     let wp = this.request.waypoints.slice();
  35.     wp[value.index].location = value.value;
  36.     this.changeValue.emit(
  37.       {...this.request, waypoints: wp }
  38.     );
  39.     */
  40.     let newObj = JSON.parse(JSON.stringify(this.request));
  41.     newObj.waypoints[value.index].location=value.value;
  42.     this.changeValue.emit(newObj);
  43.  
  44.   }
  45.  
  46. }
  47.  
  48.  
  49.  
  50. @Component({
  51.   selector: 'app-input-element',
  52.   template: `
  53.     <input type="text" [value]="value" #myinput>
  54.   `
  55. })
  56.  
  57. export class InputElementComponent{
  58.   @ViewChild('myinput', { static: true }) inputElement: ElementRef;
  59.   @Input('value') value: string;
  60.   @Input('index') index:number;
  61.   @Output('change-value') changeValue = new EventEmitter<any>();
  62.  
  63.   ngAfterViewInit(){
  64.     fromEvent(this.inputElement.nativeElement, 'keyup')
  65.             .pipe(
  66.                 map((event: KeyboardEvent) => (event.target as HTMLInputElement).value),
  67.                 filter(text => text.length > 3)
  68.             )
  69.             .subscribe(value => {
  70.         this.changeValue.emit({value:value,index:this.index});
  71.             });
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement