3

ViewChild is not woring in Angular 2 RC 1 JavaScript. I have used <router-outlet>. Can anyone help me to solve this issue? Thanks in advance. Following is my sample code

app.AppComponent = ng.core.
  Component({
    selector: "app", 
    template:
     '<div>' +
     '  <router-outlet></router-outlet>' +
     '  <div (click)="submit()">Submit</div>' +
     '</div>',
  queries: { 'viewChild1Component': new ng.core.ViewChild(app.Child1Component) },
  directives: [ng.router.ROUTER_DIRECTIVES]
})
.Class({
  submit: function() {
    console.log('#### submit');
    this.viewChild1Component.onSubmit();
  }
});

ng.router
        .Routes([
          { path: '/child1', component: app.Child1Component, useAsDefault: true },
        ])(app.AppComponent);
Abraham Gnanasingh
  • 837
  • 2
  • 10
  • 31
  • Just use a shared service to communicate with components added by the router. I guess `@ViewChild` is not supposed to work in this case. – Günter Zöchbauer Jun 01 '16 at 16:47
  • can you see the same [plunker](https://plnkr.co/edit/BEzo8eH4b4NChHKZppGd?p=preview) ? i have updated with `@ViewChild`. clicking on Alert button in AppComponent doesn't trigger alert method in LoginComponent. – Abraham Gnanasingh Jun 01 '16 at 16:55
  • As mentioned above, I would not expect this to work. – Günter Zöchbauer Jun 01 '16 at 16:58
  • but in previous versions, this worked right? even i have tried it in previous versions. also can you explain me when we use `@ViewChild`? – Abraham Gnanasingh Jun 01 '16 at 17:01
  • I never tried with routed components. You can use it to get a reference to components and elements in the view. If it worked before, I would create a bug report. – Günter Zöchbauer Jun 01 '16 at 17:05
  • can you check this [post](http://stackoverflow.com/questions/35847818/viewchild-angular-2-javascript?lq=1). i have asked before and i got the answer even for ``. i am damn sure about that the `@ViewChild` was worked well before. – Abraham Gnanasingh Jun 01 '16 at 17:17
  • For me, it's not working even without a router. That's about querying components. However, querying directives works. – Namek Jun 02 '16 at 17:02

0 Answers0