1

So I'm new to angular two and i've looked at several tutorials trying to figure out how to do this and I can't get it to work. Can you tell me what I'm doing wrong here?

So I'm trying to pass a boolean value from one component to another, which will trigger an animation with ng-class. The event happens in the child component. I need the parent to respond.

Child component:

export class DefaultRouteViewComponent implements OnInit {
  @Input() compare: boolean = false;
  @Output() compareEvent = new EventEmitter();

  constructor(public _menu: MenuService) {}

  toggleCompare () {
    this.compare = !this.compare;
    this.compareEvent.emit({
       value: this.compare
    })
  }

Parent component:

@Component({
  moduleId: module.id,
  selector: 'app-root',
  template: '<div class="app-wrapper" [ngClass]="{'hide-app' : hideApp}" (hideApp)="hideAppChange($event);" (compareEvent)="hideAppChange($event)"></div>',
  directives: NgClass, DefaultRouteViewComponent],
})
export class AppComponent implements OnInit {
  hideApp: Boolean;

  constructor() {}

  hideAppChange(event) {
     console.log(event);
  }
}

I feel like the problem is in the parent component template. I'm not sure though. Please help! Thanks.

3
  • What is the selector of your child component and its usage in parent template? Commented Aug 3, 2016 at 22:13
  • selector: 'app-default-route-view', is the selector. The parent is the header. The child is just a template for the route. Commented Aug 3, 2016 at 22:47
  • Not sure if Output is working in that case then... Commented Aug 3, 2016 at 23:27

2 Answers 2

1

The 'compareEvent' event binding needs to be placed as an attribute on the selector for the child component in the parent component's html.

So in the parent component template html you need something like:

<app-default-route-view (compareEvent)="hideAppChange($event)">
</app-default-route-view>

Hope that helps!

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

Comments

0

possible duplicate Passing @Input and subscribing to @Output while navigating to a Route in Angular 2 Component

If you need to communicate between Parent and child routed components you may use Bi Directional service

you may see similar implementation here

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.