2016-09-01 4 views
0

У меня есть родительский и дочерний компонент. Детский компонент имеет переменные шаблона, и я хочу их прочитать в родительском.angular2 как просмотреть переменную шаблона, найденную в дочернем компоненте

Родитель:

import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; 
import {Page} from "ui/page"; 
import {ChildComponent} from '/components/child/child.component' 

@Component({ 
selector: "parent", 
template: "<child [List]="List"></child>", 
directives: [ChildComponent] 
}) 

export class ParentComponent implements OnInit { 

// list contains selectable items (<a> tag in HTML) 
@ViewChildren("item") 
itemsList: QueryList<ElementRef>; 
} 

ребенок:

import {Component, OnInit, ViewChild, Input} from '@angular/core'; 
import {Observable} from 'rxjs/Observable'; 

@Component({ 
selector: "child", 
templateUrl: "components/child/child.html", 
}) 

export class ChildComponent implements OnInit { 
    @Input() list: array; 
    ..... 
} 

шаблон: child.html

<li *ngFor="let item of list" style="cursor: default;"> 
    <a (click)="handleClick()" #item > item </a> 
</li> 

можно получить список пунктов в родительских ??

ответ

1

Вы можете передавать данные в и из вложенного компонента.

Если вы хотите передать данные от дочернего к родительскому, вы можете использовать событие emitter.

См. Эту статью Passing data to and from a nested component in Angular 2

Смежные вопросы