【发布时间】:2020-05-10 21:04:48
【问题描述】:
问题陈述:有一个引导列表组。 “活动”类突出显示列表组中的选定项目。在加载组件(角度)时,应该预先选择几个列表项,这是我面临的问题。
HTML代码如下:
<div class="list-group" *ngFor="let role of roles | filter : searchText">
<a id="{{role}}" class="list-group-item list-group-item-info list-group-item-action" (click)="selectRole(role)">
{{ role }}
</a>
</div>
ts 文件如下:
roles = ["Admin", "Developer", "Tester", "Production Support", "Marketing", "Admin1", "Developer1", "Tester1", "Production Support1", "Marketing1", "2Admin", "2Developer", "2Tester", "2Production Support", "2Marketing"]
selectedRoles = ["Developer", "Marketing"]
userForm = this.fb.group({
name: [''],
roles: this.fb.array([])
})
ngOnInit( ): void {
this.patchRole()
console.log(this.userForm.value)
}
patchRole(){
console.log('patch value')
this.userForm.patchValue({roles: this.selectedRoles});
let elements = document.getElementsByClassName('list-group-item')
console.log(elements)
for (let i=0; i<this.roles.length; i++){
for (let j=0; j<this.selectedRoles.length; j++){
if (this.roles[i] === this.selectedRoles[j]){
elements[i].className += ' ' + "active"
}
}
}
}
你能帮忙吗?
【问题讨论】:
标签: html angular getelementbyid