[英]How to access data from child component? Angular2
I have created component named app-inline-edit
and it is supposed to be used like this: 我已经创建了名为app-inline-edit
组件,它应该像这样使用:
<app-inline-edit (onSave)="saveEditCheck($event)">
<app-select formControlName="CurrentCar" [options]="carOptions"></app-select>
</app-inline-edit>
By default it should show value under "CurrentCar" as plain string with pencil icon next to it. 默认情况下,它应在“ CurrentCar”下以纯字符串形式显示值,旁边带有铅笔图标。 If user clicks on string or icon it should render "app-select" component with two icons: one for saving changes, the other for discarding them. 如果用户单击字符串或图标,则应使用两个图标来呈现“ app-select”组件:一个用于保存更改,另一个用于丢弃更改。
EDIT: Child of app-inline-edit
does not always be the app-select
, it can be also different component. 编辑: app-inline-edit
并不总是app-select
,它也可以是不同的组件。 This is the template I have: 这是我的模板:
<div>
<div *ngIf="editing">
<div #inlineEditControl class="inline-edit-input">
<ng-content></ng-content>
</div>
<i class="fa fa-check" aria-hidden="true" (click)="save()"></i>
<i class="fa fa-times" aria-hidden="true" (click)="discard()"></i>
</div>
<div *ngIf="!editing">
<div title="Click to edit" (focus)="edit(value);" tabindex="0" class="inline-edit">
{{value}} <i class="fa fa-pencil" aria-hidden="true" (click)="edit(value)"></i>
</div>
</div>
</div>
I've tried to access it via 我试图通过访问它
@ViewChild('inlineEditControl') inlineEditControl: ElementRef;
But when editing
is false
inlineEditControl
is undefined
. 但是,如果editing
为false
inlineEditControl
是undefined
。 The problem I've run into is. 我遇到的问题是。 How can I access selected value (CurrentCar) attached to "app-select" if by default select is not rendered? 如果默认情况下未呈现选择,如何访问附加到“ app-select”的选择值(CurrentCar)? Is it even possible? 可能吗?
You should be using @ViewChild
to that 您应该使用@ViewChild
来
In your component 在你的组件中
@ViewChild<AppSelectComponent> appSelectComponent: AppSelectComponent;
this.appSelectComponent.options /////////////// have it here
<app-select formControlName="CurrentCar" [options]="carOptions"></app-select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.