Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2.  
  3. @Injectable()
  4. export class AppService {
  5. messages: string[] = [];
  6.  
  7. updateMessages(msg: string) {
  8. this.messages.push(msg);
  9. }
  10. }
  11.  
  12. import { Component, OnInit } from '@angular/core';
  13. import { AppService } from './app.service';
  14. import { ChildComponent } from './child.component';
  15. import { IsolatedComponent } from './isolated.component';
  16.  
  17. @Component({
  18. selector: 'my-app',
  19. template: `
  20. <h1>AppComponent Tree</h1>
  21. <p>
  22. AppComponent and ChildComponent consume the same instance of AppService
  23. </p>
  24. <child-component></child-component>
  25. <hr />
  26. <isolated-component></isolated-component>
  27. `,
  28. providers: [AppService],
  29. directives: [ChildComponent, IsolatedComponent]
  30. })
  31. export class AppComponent implements OnInit {
  32. messages: string[];
  33.  
  34. constructor(private appService: AppService) {
  35. this.messages = appService.messages;
  36. }
  37.  
  38. ngOnInit() {
  39. this.addMessage(`AppComponent Initialized`);
  40. }
  41.  
  42. private addMessage(msg: string) {
  43. this.appService.updateMessages(msg);
  44. }
  45. }
  46.  
  47. import { Component, OnInit } from '@angular/core';
  48. import { AppService } from './app.service';
  49.  
  50. @Component({
  51. selector: 'child-component',
  52. template: `
  53. <div *ngFor="let message of messages">{{message}}</div>
  54. `
  55. })
  56. export class ChildComponent implements OnInit {
  57. messages: string[];
  58.  
  59. constructor(private appService: AppService) {
  60. this.messages = appService.messages;
  61. }
  62.  
  63. ngOnInit() {
  64. this.addMessage(`ChildComponent Initialized`);
  65. }
  66.  
  67. private addMessage(msg: string) {
  68. this.appService.updateMessages(msg);
  69. }
  70. }
  71.  
  72. import { Component, OnInit } from '@angular/core';
  73. import { AppService } from './app.service';
  74. import { IsolatedChildComponent } from './isolated-child.component';
  75.  
  76. @Component({
  77. selector: 'isolated-component',
  78. template: `
  79. <h1>Isolated Component Tree</h1>
  80. <p>
  81. IsolatedComponent and IsolatedChildComponent consume an
  82. instance of AppService separate from the AppComponent tree
  83. </p>
  84. <isolated-child></isolated-child>
  85. `,
  86. providers: [AppService],
  87. directives: [IsolatedChildComponent]
  88. })
  89. export class IsolatedComponent implements OnInit {
  90. messages: string[];
  91.  
  92. constructor(private appService: AppService) {
  93. this.messages = appService.messages;
  94. }
  95.  
  96. ngOnInit() {
  97. this.addMessage(`IsolatedComponent initialized`);
  98. }
  99.  
  100. private addMessage(msg: string) {
  101. this.appService.updateMessages(msg);
  102. }
  103. }
  104.  
  105. import { Component, OnInit } from '@angular/core';
  106. import { AppService } from './app.service';
  107.  
  108. @Component({
  109. selector: 'isolated-child',
  110. template: `
  111. <div *ngFor="let message of messages">{{message}}</div>
  112. `
  113. })
  114. export class IsolatedChildComponent implements OnInit {
  115. messages: string[];
  116.  
  117. constructor(private appService: AppService) {
  118. this.messages = appService.messages;
  119. }
  120.  
  121. ngOnInit() {
  122. this.addMessage(`IsolatedChildComponent initialized`);
  123. }
  124.  
  125. private addMessage(msg: string) {
  126. this.appService.updateMessages(msg);
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement