gpt4 book ai didi

html - 从一个组件访问另一个组件的方法

转载 作者:行者123 更新时间:2023-12-02 07:16:54 26 4
gpt4 key购买 nike

我在 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>

最佳答案

你应该使用 ViewChild在父组件中:

import { ViewChild } from '@angular/core';

@ViewChild('popName', { static: true }) popName: PopoverComponent;

然后你可以这样调用你的函数:

this.popName.toggleWithGreeting("", "");

关于html - 从一个组件访问另一个组件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60489837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com