簡體   English   中英

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>

可以在父項??中獲得項目列表。

您可以在嵌套組件之間來回傳遞數據。

如果要將數據從子級傳遞到父級,則可以使用事件發射器。

請參閱本文在Angular 2中與嵌套組件之間傳遞數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM