2

I'm trying to have multiple angular components subscribe to a single http service. he flow of events is: User clicks button in test-data.component.html > test-data.service makes POST request > subscription in test-data.component receives data AND parent.component.ts receives data. The issue is that parent.component.ts never recieves that new data from the POST. Do I need to notify the parent.component that changes happened in the service provider? Here are the relevant parts of the application:

parent.component.html

<div>  
 <app-test-data></app-test-data>
</div>

parent.component.ts

@Component({
 selector: 'app-short-form',
 templateUrl: './short-form.component.html',
 styleUrls: ['./short-form.scss'],
 providers:[ShortFormService, TestDataService]
})

constructor(
 private testDataService: TestDataService,
) { }

ngOnInit() {   
 this.testDataService.getTestData()
  .subscribe(
    (event: HttpEvent<any>) => {
      switch (event.type) {
        case HttpEventType.Response:
          console.log('😺 Done!', event.body);
      }
    },
    (err: HttpErrorResponse) => {
      if (err.error instanceof Error) {
        console.log("Client-side error occured.");
      } else {
        console.log("Server-side error occured.");
      }
    }
  )

}

test-data.component.html

<button m(click)="getTestData($event)" >Populate w/ match</button>

test-data.component.ts

@Component({
  selector: 'app-test-data',
  templateUrl: './test-data.component.html',
  styleUrls: ['./test-data.component.scss'],
  providers: [TestDataService]

})
constructor(
 private testDataService: TestDataService,
) { }

getTestData(event){
 event.preventDefault();
 this.testDataService.getTestData()
 .subscribe(     
  (event: HttpEvent<any>) => {
    switch (event.type) {
      case HttpEventType.Response:
        console.log('😺 Done!', event.body);          
    }
  },
  (err: HttpErrorResponse) => {
    if (err.error instanceof Error) {
      console.log("Client-side error occured.");
    } else {`enter code here`
      console.log("Server-side error occured.");
    }
  }
)
}

test-data.service.ts

constructor(
 private http:HttpClient
) { }

getTestData(): Observable<HttpEvent<any>> {
  const req = new HttpRequest('POST', this.testDataUrl, {
    reportProgress: true,
  });
var result = this.http.request(req).share();
return result
1
  • 1
    you need to have a varaible in that service like a behavioursubject or replaysubject that will notify the parent component. This is a classic case for you to use shared services to avoid event spaghetti or use ngrx . ngrx link shared services link Commented Aug 9, 2017 at 18:15

1 Answer 1

1

You should use Subject or BehaviourSubject and make your compometes subscribe to it. You can find more about these topic in this link: http://jasonwatmore.com/post/2016/12/01/angular-2-communicating-between-components-with-observable-subject

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent resource in your link! I got it working based on the example given. What is the purpose of having the messaging.service separate? Reusability? My initial approach was trying to bake the messaging.service functionality into my test-data.service
Thank you, the purpose of using services in angular is separation of concerns and reusability. you do not need to apply the message service in your code, just understand the way Subject works and implemented in your test-data.service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.