A
A
Aleksandr2019-07-18 17:02:38
Angular
Aleksandr, 2019-07-18 17:02:38

How to implement data passing between two components in Angular 7?

There are two neighboring components

<b2b-acquiring *ngIf="resultSearchByNumber" [inputData]="resultSearchByNumber"></b2b-acquiring>
<b2b-restart-business-process></b2b-restart-business-process>

The component b2b-acquiringhas a method that sends a request to the server and here is the response that comes up, I need to pass it to the b2b-restart-business-process
Send method
getPayInfoExtended(obj: any) {
    this.resultSearchByNumber = null;
    this.resultSearchByNumber = obj;
    this.resultGetPayInfoExtended = null;
    return this.http.post(`${this.baseUrl}`+'getpayinfoextended?', this.resultSearchByNumber).subscribe(res => {
        this.resultGetPayInfoExtended = res;
    },
    error => {
        console.log(error)
    })
}

I read about @Outputit but I don't understand how it works. Explain on your fingers please
UPDATE
I tried via @Output
In componentb2b-acquiring
@Output() payInfo = new EventEmitter();

sendDateToParent() {
    this.payInfo.emit(this.resultGetPayInfoExtended);
}

In parent component
payInfoTest(evnt) {
    this.payInfoData = null;
    this.payInfoData = evnt;
    console.log(this.payInfoData)
}

<b2b-acquiring *ngIf="resultSearchByNumber" [inputData]="resultSearchByNumber" (payInfo)="payInfoTest($event)"></b2b-acquiring>

In the console it displays what I need
. How can I now transfer this case to another component b2b-restart-business-process?
Tried via @Input
In the right component@Input() inputData2;
<b2b-restart-business-process [inputData2]="payInfoData"></b2b-restart-business-process>

And I'm trying to display the data. {{ inputData2 | json }}
But the reactions are 0, what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-07-18
@Sashjkeee

It's not how it's done at all.
The service should deal with this, the components are representation classes, there should not be work with data.
The service has a subject with data and a data request method that returns an observable, throwing data into the subject along the way.
The first component pulls the method and subscribes to it, thereby launching it for execution.
The second component is subscribed to the subject, best of all through the async pipe.
@Output() in the first component, if you want to use it, it should not show data, but an event on which the parent component will request data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question