如何通过 ViewChild 名称获取@ViewChild() 的关联对象

时间:2021-03-02 09:18:52

标签: javascript angular angular6

我有一个表单元素的 ViewChild 声明:

@ViewChild('callerForm') callerFormObj: any;

实际上我在这个网页上有多个表单,它们都有自己的 ViewChild 和关联对象

我的问题是:如果我有表单的名称,我能否以某种方式获得对 ViewChild 对象的引用:callerFormObj?

// Get control associated with property
let anElement = document.querySelector('[ng-reflect-name="' + propName + '"]'); // ng-reflect-name
// If control exists continue
if (anElement) {
  // Find controls associated form element
  let aForm = anElement.closest('form');
  if (aForm) {
    let formName = aForm.name; // e.g. for ViewChild above will be 'callerForm'
    // Get forms viewchild associated obj
    let formViewChildObj = ??????; // A reference to callerFormObj
  }
}

不用 jquery,谢谢!

0 个答案:

没有答案
相关问题