[英]How to access Child Component HTML Element values in Parent Component in angular2?
在父组件的按钮单击期间,我使用下面的代码访问父组件中的子组件HTML元素值。
儿童组件: -
@Component({
selector: 'child-component',
template: `<md-input-container><label>Title</label><input [(ngModel)]="Title" #Title></md-input-container><md-input-container><label>Name</label><input [(ngModel)]="name" #Name></md-input-container><md-input-container class="md-block" flex-gt-sm><label>State</label><md-select [(ngModel)]="user.state" #State><md-option *ngFor="let state in states" value="{{state.abbrev}}">{{state.abbrev}}</md-option></md-select></md-input-container>`
})
export class ChildComponent {
// some code here
}
父组件: -
import {
Directive,
EventEmitter,
Output,
OnInit,
ElementRef
} from '@angular/core';
@Component({
selector: 'parent-component',
template: `<child-component></child-component><button class="md-button md-ink-ripple" type="submit" (click)="SaveCustomerDetails()"><span class="ng-scope">Submit</span></button>`
})
export class ParentComponent {
@ViewChild('Title') title:ElementRef;
@ViewChild('State') state:ElementRef;
Title: string;
State: string;
SaveCustomerDetails() {
this.Title = this.title.nativeElement.value; // undefined
this.State= this.state.nativeElement.innerHTML; // undefined
}
}
Plunker: - https://plnkr.co/edit/r3237ta1XzhM2PX09pMl?p=preview
但我无法在SaveCustomerDetails函数中获取子组件的HTML元素值。 如何使用ViewChild方法在父组件中输入值?
如果组件具有真正的父/子关系(一个嵌套在另一个中),则可以使用属性上的@Input和@Output装饰器在两个组件之间进行通信。
我有一篇关于此的博客文章: https : //blogs.msmvps.com/deborahk/passing-data-to-and-raising-an-event-from-a-nested-component/
以下是带@Input和@Output装饰器的子组件示例:
import { Component, OnChanges, Input, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'pm-star',
templateUrl: './star.component.html',
styleUrls: ['./star.component.css']
})
export class StarComponent implements OnChanges {
@Input() rating: number;
starWidth: number;
@Output() ratingClicked: EventEmitter<string> =
new EventEmitter<string>();
ngOnChanges(): void {
this.starWidth = this.rating * 86 / 5;
}
onClick(): void {
this.ratingClicked.emit(`The rating ${this.rating} was clicked!`);
}
}
你可以在这里找到完整的例子: https : //github.com/DeborahK/Angular-GettingStarted
在任何其他情况下,您可以构建服务以在组件之间进行通信,如下所示:
@Injectable()
export class ChildDataService {
title: string;
name: string;
}
有关更多信息,请参阅此plunker: https ://plnkr.co/edit/iODMVQzYwXcf5qJRR1El?p = preview
并不是说我在这里提倡使用@ViewChild(它是一个紧密耦合的解决方案),但是如果你想在不使用@Output属性的情况下这样做,它可能是:
@Component({
selector: 'child-component',
template: `<input (change)='title=$event.target.value'>
<input (change)='name=$event.target.value'>`
})
export class ChildComponent {
title = "";
name = "";
}
@Component({
selector: 'parent-component',
template: `<child-component #child></child-component><button type="submit" (click)="SaveCustomerDetails()">Submit</button>`,
})
export class ParentComponent {
@ViewChild('child') myChild: ChildComponent;
SaveCustomerDetails(){
console.log(this.myChild.title + "" + this.myChild.name);
}
}
}
我在这里修改了你的plunker: https ://plnkr.co/edit/mCGcIW1AVX2e9gEBzeC0 ? p = preview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.