我已经创建了一个子组件,其中有一个我想调用的方法。
当我调用这个方法时,它只会触发console.log()行,它不会设置测试属性??
下面是我修改后的Angular快速入门应用。
父
import { Component } from '@angular/core';
import { NotifyComponent } from './notify.component';
@Component({
selector: 'my-app',
template:
`
<button (click)="submit()">Call Child Component Method</button>
`
})
export class AppComponent {
private notify: NotifyComponent;
constructor() {
this.notify = new NotifyComponent();
}
submit(): void {
// execute child component method
notify.callMethod();
}
}
孩子
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'notify',
template: '<h3>Notify {{test}}</h3>'
})
export class NotifyComponent implements OnInit {
test:string;
constructor() { }
ngOnInit() { }
callMethod(): void {
console.log('successfully executed.');
this.test = 'Me';
}
}
我如何设置测试属性以及?
我认为最简单的方法是使用Subject。在下面的示例代码中,每次调用'tellChild()'时都会通知子进程。
Parent.component.ts
import {Subject} from 'rxjs/Subject';
...
export class ParentComp {
changingValue: Subject<boolean> = new Subject();
tellChild() {
this.changingValue.next(true);
}
}
Parent.component.html
<my-comp [changing]="changingValue"></my-comp>
Child.component.ts
...
export class ChildComp implements OnInit{
@Input() changing: Subject<boolean>;
ngOnInit(){
this.changing.subscribe(v => {
console.log('value is changing', v);
});
}
}
斯塔克布利茨的工作样本
这对我很管用!对于Angular 2,在父组件中调用子组件方法
Parent.component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { ChildComponent } from '../child/child';
@Component({
selector: 'parent-app',
template: `<child-cmp></child-cmp>`
})
export class parentComponent implements OnInit{
@ViewChild(ChildComponent ) child: ChildComponent ;
ngOnInit() {
this.child.ChildTestCmp(); }
}
Child.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'child-cmp',
template: `<h2> Show Child Component</h2><br/><p> {{test }}</p> `
})
export class ChildComponent {
test: string;
ChildTestCmp()
{
this.test = "I am child component!";
}
}
我有一个确切的情况,父组件在表单中有一个Select元素,在提交时,我需要根据从选择元素中选择的值调用相关的子组件的方法。
Parent.HTML:
<form (ngSubmit)='selX' [formGroup]="xSelForm">
<select formControlName="xSelector">
...
</select>
<button type="submit">Submit</button>
</form>
<child [selectedX]="selectedX"></child>
的父母。TS:
selX(){
this.selectedX = this.xSelForm.value['xSelector'];
}
的孩子。TS:
export class ChildComponent implements OnChanges {
@Input() public selectedX;
//ngOnChanges will execute if there is a change in the value of selectedX which has been passed to child as an @Input.
ngOnChanges(changes: { [propKey: string]: SimpleChange }) {
this.childFunction();
}
childFunction(){ }
}
希望这能有所帮助。
User6779899的答案简洁且更通用
但是,根据Imad El Hitti的要求,这里提出了一种轻量级的解决方案。当子组件仅与一个父组件紧密连接时,可以使用此选项。
Parent.component.ts
export class Notifier {
valueChanged: (data: number) => void = (d: number) => { };
}
export class Parent {
notifyObj = new Notifier();
tellChild(newValue: number) {
this.notifyObj.valueChanged(newValue); // inform child
}
}
Parent.component.html
<my-child-comp [notify]="notifyObj"></my-child-comp>
Child.component.ts
export class ChildComp implements OnInit{
@Input() notify = new Notifier(); // create object to satisfy typescript
ngOnInit(){
this.notify.valueChanged = (d: number) => {
console.log(`Parent has notified changes to ${d}`);
// do something with the new value
};
}
}