我已经创建了一个子组件,其中有一个我想调用的方法。
当我调用这个方法时,它只会触发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';
}
}
我如何设置测试属性以及?
我有一个确切的情况,父组件在表单中有一个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(){ }
}
希望这能有所帮助。
Angular——在父组件模板中调用子组件的方法
你有像这样的ParentComponent和ChildComponent。
parent.component.html
parent.component.ts
import {Component} from '@angular/core';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent {
constructor() {
}
}
child.component.html
<p>
This is child
</p>
child.component.ts
import {Component} from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent {
constructor() {
}
doSomething() {
console.log('do something');
}
}
发球时,是这样的:
当用户关注ParentComponent的input元素时,你需要调用ChildComponent的doSomething()方法。
简单地这样做:
给parent.component.html中的app-child selector一个DOM变量名
(前缀# -标签),在这种情况下,我们称之为appChild。
将表达式值(要调用的方法的值)赋给输入元素的焦点事件。
结果:
我有一个确切的情况,父组件在表单中有一个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(){ }
}
希望这能有所帮助。
你可以使用@ViewChild来查看更多信息
使用类型选择器
子组件
@Component({
selector: 'child-cmp',
template: '<p>child</p>'
})
class ChildCmp {
doSomething() {}
}
父组件
@Component({
selector: 'some-cmp',
template: '<child-cmp></child-cmp>',
directives: [ChildCmp]
})
class SomeCmp {
@ViewChild(ChildCmp) child:ChildCmp;
ngAfterViewInit() {
// child is set
this.child.doSomething();
}
}
使用字符串选择器
子组件
@Component({
selector: 'child-cmp',
template: '<p>child</p>'
})
class ChildCmp {
doSomething() {}
}
父组件
@Component({
selector: 'some-cmp',
template: '<child-cmp #child></child-cmp>',
directives: [ChildCmp]
})
class SomeCmp {
@ViewChild('child') child:ChildCmp;
ngAfterViewInit() {
// child is set
this.child.doSomething();
}
}
parent.component.html
<app-child #childComponent></app-child>
parent.component.ts
@Component({
selector: 'app-parent',
templateUrl: './app-parent.component.html',
styleUrls: ['./app-parent.component.scss']
})
export class ParentComponent {
@ViewChild('childComponent', {static: false}) childComponent: ChildComponent;
anyMethod(): void {
childComponent.updateData() // updateData is a child method
}
}
child.component.ts
@Component({
selector: 'app-child',
templateUrl: './app-child.component.html',
styleUrls: ['./app-child.component.scss']
})
export class ChildComponent {
updateData(): void {
// Method code goes here
}
}