角度:如何使用打字稿变量控制选择元素的"Size"属性?



我正在尝试替换size="6〃具有大小=";this.groupsList.length">但它不起作用。如何做到这一点?

<select name="groups" size="6">
<option *ngFor="let group of groupsList">{{group.name}}</option>
</select>  

背景:

我这样做是因为如果元素的#是>";尺寸";,但是由于我需要使用ngx-perfect滚动条;尺寸";属性。由于我使用的是ngx-perfect滚动条,我需要隐藏select标签的滚动条,但这样做也会隐藏一些组名(如果"size"属性小于groupsList的长度(

您只需要这样做:

<select name="groups" [attr.size]="groupsList.length">
<option *ngFor="let group of groupsList">{{group.name}}</option>
</select>  

为了解决这个问题,我在.ts文件中添加了以下内容:

const selectTag = document.getElementById('groupsListSelect');
selectTag.setAttribute("size",this.groupsList.length+"")

并添加";id=";groupsListSelect;在html:中

<select id="groupsListSelect" name="groups" size="1">
<option *ngFor="let group of groupsList">{{group.name}}</option>
</select>  

它在groupsList初始化后立即用上面的typescript代码覆盖大小1。

最新更新