angular2 需要访问父 class 中的子值

angular2 need to access the child value in parent class

在这里,我需要访问父 class 中的子值。 但我无法得到它。如果我使用指令,它会显示错误。 有人可以帮我在父组件中显示子值以及如何以反应形式验证子值吗?

parent.html :

<browser  [template]="coreActivity" (onselect)="onselect($event)" ></browser>

parent.ts :

 onselect(select: any)
 {
    console.log(select.value);
 }

child.html :

<md-grid-list cols="3" rowHeight="100px">
    <md-grid-tile *ngFor="let core of template" (click)="selectcore(core)">
       {{core.value}}
     </md-grid-tile>
</md-grid-list>

child.ts :

@Component({
  selector: 'template-browser',
  templateUrl: './template-browser.component.html',
  styleUrls: ['./template-browser.component.css']
})
export class TemplateBrowserComponent implements OnInit {
  @Input() template;

  @Output() notify: EventEmitter<string> = new EventEmitter<string>();  
  constructor(private community: CreateCommunityComponent ) { }  
  selectcore(core: any) {
      // alert(core.value);
      this.notify.emit(core.value);
    }  
}

为了在父级中访问 child.html,您需要创建另一个组件并将该组件名称用作父级中的标记。 像组件名称是 "child" 将 <child></child> 保留在父

当您通过 EventEmitter 传递值时,您需要在 parent.html 中通过子组件声明传递它。

<browser (notify)="onSelect($event)"  [template]="coreActivity" (onselect)="onselect($event)" ></browser>

并且在parent.ts下的函数中:

 onSelect(select)
 {
    console.log(select.value);
 }