javascript - Unable to catch event emitted from child component in Angular 4 - Stack Overflow

I am sorry for my newbie question, I am trying to emit an event from a child to a parent ponent using a

I am sorry for my newbie question, I am trying to emit an event from a child to a parent ponent using an @Output EventEmitter. I am unable to catch the event in my parent ponent.

Child Component

@Component({
  selector: 'app-new-attachment',
  templateUrl: './new-attachmentponent.html',
  styleUrls: ['./new-attachmentponent.css']
})
class NewAttachmentComponent {
  @Input('attachment') attachment: any;
  @Output() doneEditingAttachment:EventEmitter<boolean> = new EventEmitter<boolean>();

  submit() {
    console.log('done editing child');
    this.doneEditingAttachment.emit(true);
  }
}

Parent Component

@Component({
  selector: 'app-new-article',
  templateUrl: './new-articleponent.html',
  styleUrls: ['./new-articleponent.css']
})
class NewArticleComponent {
   newAttachment: any = {};
   doneEditingAttachment($event) {
     console.log('done editing parent ', $event);
   }
}

I expected to have

done editing child

And a

done editing parent

But I only got done editing child

I am sorry for my newbie question, I am trying to emit an event from a child to a parent ponent using an @Output EventEmitter. I am unable to catch the event in my parent ponent.

Child Component

@Component({
  selector: 'app-new-attachment',
  templateUrl: './new-attachment.ponent.html',
  styleUrls: ['./new-attachment.ponent.css']
})
class NewAttachmentComponent {
  @Input('attachment') attachment: any;
  @Output() doneEditingAttachment:EventEmitter<boolean> = new EventEmitter<boolean>();

  submit() {
    console.log('done editing child');
    this.doneEditingAttachment.emit(true);
  }
}

Parent Component

@Component({
  selector: 'app-new-article',
  templateUrl: './new-article.ponent.html',
  styleUrls: ['./new-article.ponent.css']
})
class NewArticleComponent {
   newAttachment: any = {};
   doneEditingAttachment($event) {
     console.log('done editing parent ', $event);
   }
}

I expected to have

done editing child

And a

done editing parent

But I only got done editing child

Share asked Aug 8, 2017 at 13:03 jemlifathijemlifathi 1,6025 gold badges23 silver badges33 bronze badges 2
  • 1 how does the html look like? – Sajeetharan Commented Aug 8, 2017 at 13:03
  • @Sajeetharan <app-new-attachment [hidden]="!toggleNewAttachment" [attachment]="newAttachment"></app-new-attachment> I already figured it out: I forgot to add the event in the child html – jemlifathi Commented Aug 8, 2017 at 13:04
Add a ment  | 

2 Answers 2

Reset to default 6

You need to actually have a binding to the event of the child using the (eventName) notation:

<app-new-attachment [attachment]="newAttachment" (doneEditingAttachment)="doneEditingAttachment($event)"></app-new-attachment>

Based on the mnet you need this,

<app-new-attachment [attachment]="newAttachment" (doneEditingAttachment)="submit()"></app-new-attachment>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745049787a4608316.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信