Guest User

Untitled

a guest
Jan 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import { Component, OnChanges, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
  2. import { timer } from 'rxjs';
  3.  
  4. @Component({
  5. selector: 'app-root',
  6. template: `
  7. <tooltip [input]="childInput"></tooltip>
  8. <button (click)="onClick()" >Trigger change detection</button>
  9. `,
  10. changeDetection: ChangeDetectionStrategy.OnPush
  11. })
  12.  
  13. export class AppComponent implements OnChanges {
  14.  
  15. constructor(private cd: ChangeDetectorRef){ }
  16.  
  17. childInput = {
  18. time: new Date()
  19. };
  20.  
  21. ngOnChanges() {
  22. console.log('App component on changes.');
  23. }
  24.  
  25. onClick() {
  26. timer(1000).subscribe(sub => {
  27.  
  28.  
  29.  
  30. this.childInput = {
  31. time: new Date()
  32. };
  33.  
  34. });
  35. }
  36.  
  37. }
  38.  
  39.  
  40. import { Component, ChangeDetectionStrategy, Input, OnChanges, SimpleChange, SimpleChanges, Output, ChangeDetectorRef } from '@angular/core';
  41.  
  42. @Component({
  43. selector: 'tooltip',
  44. template: `
  45. <h1>{{input.time}}</h1>
  46. `,
  47. changeDetection: ChangeDetectionStrategy.OnPush
  48. })
  49. export class TooltipComponent implements OnChanges {
  50. constructor(private cd: ChangeDetectorRef,) {}
  51.  
  52. @Input() input;
  53.  
  54. ngOnChanges(){
  55. console.log('tooltip on changes called');
  56. }
  57.  
  58. }
Add Comment
Please, Sign In to add comment