Javascript 获取角度引导中ngb选项卡集中Select DOM元素的值
我在*ngFor循环中有以下DOM元素(索引设置为idx): 当我执行Javascript 获取角度引导中ngb选项卡集中Select DOM元素的值,javascript,angular,Javascript,Angular,我在*ngFor循环中有以下DOM元素(索引设置为idx): 当我执行console.log(document.querySelector('#myID0'))时,我会打印元素。由于某些原因,我无法获得值 更新 我错过了一个事实,我的选择是在一个 你可以看到这个问题的复制品。完整的HTML代码是: <ngb-tabset> <ngb-tab> <ng-template ngbTabTitle="">Tab</ng-template>
console.log(document.querySelector('#myID0'))
时,我会打印元素。由于某些原因,我无法获得值
更新
我错过了一个事实,我的选择是在一个
你可以看到这个问题的复制品。完整的HTML代码是:
<ngb-tabset>
<ngb-tab>
<ng-template ngbTabTitle="">Tab</ng-template>
<div class="row" *ngFor="let item of myArray let idx=index">
<select [id]="'myID' + idx">
<option>Hello</option>
</select>
</div>
<ng-template ngbTabContent=""></ng-template>
</ngb-tab>
</ngb-tabset>
标签
你好
使用Angular时,我会尽量避免以这种方式访问DOM。您应该看看如何实现viewchildren。这将为您提供有关元素所需的所有信息。抱歉,自从你进行stackblitz以来,答案发生了巨大的变化
public @ViewChildren('idx')public domElement: QueryList<any>; // be sure to import.
<select #idx> // in HTML
为select元素提供一个模板引用变量,并使用
ViewChildren
在组件中获取QueryList
@ViewChildren('selectElements',{read:ElementRef})
selectElements:QueryList;
请参阅
select([id]=““myID”+idx”)
不是dom元素,而是函数调用。那么。。。它在帕格中:现在可能更清楚了。不确定typscript是否会对你大喊大叫,但你是否尝试过document.querySelector(“#myID0”)。value
Yes,尝试了同样的问题…ViewChildren
使用的是引用变量的名称,而不是id。另外,ViewChildren
属性将以this的初始值执行。requireElement
-我认为这里的想法行不通。ViewChildren不喜欢这个。requireElement反正haha stackblitz帮助测试了我的能力接近。ViewChildren可以拿起#id:)我已经更新了我的问题,这能解决问题吗?
ERROR TypeError: Cannot read property 'value' of null
<ngb-tabset>
<ngb-tab>
<ng-template ngbTabTitle="">Tab</ng-template>
<div class="row" *ngFor="let item of myArray let idx=index">
<select [id]="'myID' + idx">
<option>Hello</option>
</select>
</div>
<ng-template ngbTabContent=""></ng-template>
</ngb-tab>
</ngb-tabset>
public @ViewChildren('idx')public domElement: QueryList<any>; // be sure to import.
<select #idx> // in HTML
const test = this.domElements.find(element => element.yourvalue === whatYouExpect);