undefinedfix
Sign in

The value transfer problem of angular4 / 5

Adephx edited in Wed, 03 Nov 2021

This option is passed in, no problem. I want to send out searchtags without event emission and the same service. How can I bind the searchtags property to the properties of the parent component?

图片描述

4 Replies
yched
commented on Wed, 03 Nov 2021

If you don't need an event, how can you notify the parent component? Let's use the event output @ output optionschange = new EventEmitter < number > ();

eremmel
commented on Wed, 03 Nov 2021

Communication mode between components:

The parent component passes data to the child component. Through the @ input property defined in the child component, input parameters are as follows:

子组件中:
@Input() options: any;

父组件中:
<father>
    <son [options]="opt"></son>
</father>

The child component transfers data to the parent component and outputs events through the @ output EventEmitter defined in the child component

子组件中:
<button (click)="onClick($event)">Click</button>
@Input() options: any;
@Output() changed: EventEmitter<any> = new EventEmitter<any>();

onClick(value) {
   this.changed.next(value);
}

父组件中:
<father>
    <son [options]="opt" (changed)="handleChanged($event)"></son>
</father>

handleChanged(value) {
    console.info(value);////
}
user282
commented on Thu, 04 Nov 2021

You don't need the transmission of parent-child components, and you don't need the same service. Don't you block the way... Define service a to store data, and define service B to get data from service a

itsmnthn
commented on Thu, 04 Nov 2021

export class ChildComponent{
    constructor(@Inject(ParentComponent) private parent: ParentComponent){

    }
    
    searchTags:any

    someMethod(){
        this.parent.aPublicProperty = searchTags;
    }
}

Ng5 supports injection of parent components

lock This question has been locked and the reply function has been disabled.