我在 Angular 中,需要从另一个组件调用一个组件的方法。
拥有这个组件:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-popover',
templateUrl: './popover.component.html',
styleUrls: ['./popover.component.scss']
})
export class PopoverComponent implements OnInit {
constructor() {}
@Input() texto:string;
toggleWithGreeting(popover, texto: string) {
if (popover.isOpen()) {
popover.close();
} else {
popover.open({texto});
}
}
ngOnInit(): void {}
}
我需要从父组件访问toggleWithGreeting 方法,我有这个:
send(){
this.renderer.selectRootElement(this.popName.nativeElement).toggleWithGreeting("","");
}
但它不起作用,我无法访问该方法,如果您可以提供帮助,则 html 是这样的:
<app-popover #popName [texto]="'Esto va a ser el mensaje de error'" ></app-popover>
你应该使用查看子对象 https://angular.io/api/core/ViewChild在父组件中:
import { ViewChild } from '@angular/core';
@ViewChild('popName', { static: true }) popName: PopoverComponent;
然后你可以像这样调用你的函数:
this.popName.toggleWithGreeting("", "");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)