【问题标题】:How to access the DOM element of some external library in a parent component where it is used in Angular如何访问在 Angular 中使用的父组件中某些外部库的 DOM 元素
【发布时间】:2022-02-11 20:13:05
【问题描述】:

我需要操纵一个外部库组件中存在的按钮的样式和可见性。为了更好地理解下面的场景。

parent.component.html <child-component></child-component>

child.component.html <external-component></external-component>

所以按钮存在于我需要在父组件中操作的外部库组件中。

注意:该按钮没有任何模板引用变量,并且由于它存在于外部库中,因此我无法将其添加到其 html 中。

有什么办法可以做到吗? 在此先感谢:-)

【问题讨论】:

    标签: html angular


    【解决方案1】:

    在组件文件中添加如下代码

    import { ElementRef } from '@angular/core';
    
    @Component({
       selector: 'app-my-contract',
       templateUrl: './my.component.html',
       styleUrls: ['./my-contract.component.css']
    })
    export class MbpContractComponent implements OnInit {
    @ViewChild('myInput') myInputVariable: ElementRef
    

    }

    在您的代码中,您可以像这样使用

    this.myInputVariable.nativeElement.disabled = true;
    

    或者您可以使用该元素上的任何属性

    【讨论】:

    • 我在我的问题中提到该按钮没有附加任何变量名,我无法添加它,因为它在外部库组件中
    • 尝试访问父元素,然后访问子元素
    猜你喜欢
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2013-10-26
    • 2017-03-03
    • 2017-11-29
    相关资源
    最近更新 更多