![](/img/trans.png)
[英]How to pass a variable from a child component template to a parent component in angular2?
[英]angular2 how to view template variable found in child component
我有一個父子組件。 子組件具有模板變量,我想將其讀入父組件。
上級:
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>
可以在父項??中獲得項目列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.