1

Whatever values are inside the individuals are printed without issues but whatever is obtained using @Input or @Output is not displayed properly.

I'm trying to assign childData in child.component.ts with parent property parentData.

Similarly I'm trying to assign child property "data" to parent property "data"

child.component.ts

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {

  @Input() public childData: string;
  @Output() public event: EventEmitter<string> = new EventEmitter();
  public data = 'Harsha';
  constructor() { }

  onClick() {
    console.log("sending value to parent:"+this.data);
    this.event.emit(this.data);
  }

  ngOnInit() {
  }

}

parent.component.ts

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {

  public parentData = 'Hello Parent';
  public data: string;

  constructor() { }

  onOutputEvent(data: string){
    console.log("in parent:"+data);
    this.data = data;
  }

}

<!-- child.component.html -->
<h3>Printing parent value in child component: {{childData}}</h3>

<h4>Printing child value:{{data}}</h4>

<button (click)="onClick()">Send value to parent</button>

<!-- parent.componnt.html -->
<h3>Printing parent Data: {{parentData}}</h3>
<h4>Printing child Data: {{data}}</h4>

<app-child> [childData]="parentData" (event)="onOutputEvent($event)"</app-child>

<!-- app.component.html -->
<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
</div>

<app-parent></app-parent>

2
  • Try <app-child [childData]="parentData" (event)="onOutputEvent($event)"></app-child>. Commented Oct 30, 2018 at 0:57
  • You are closing the tag without assigning the attributes.<app-child> Commented Oct 30, 2018 at 0:58

2 Answers 2

1

You closed the tag before assigning the attributes.

<app-child [childData]="parentData" (event)="onOutputEvent($event)"></app-child>
Sign up to request clarification or add additional context in comments.

Comments

0

You closed the app-child directive at the wrong place, try:

<app-child [childData]="parentData" (event)="onOutputEvent($event)"></app-child>

Comments

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.