ng2-select: cant set active / selected items
Hey, i am using ng2-select and i am quiet satisfied. I spent the last 3 hours to set the initial data of the select.
My first attempt was to use "active: code:
<ng-select [active]="active"...
public active = ['Amsterdam'];
Compiler:
Can’t bind to ‘active’ since it isn’t a known property of ‘ng-select’
After that I tried around with some hacks but so far I couldnt find a solution. I also want to use it as a multi select, but i am already failing on the single selection. Unfortunately there is no demo and i am out of ideas…
Another little thought: CSS height is not working correctly. I have to set the height initially but if it changes (like on multiselect), therefore the hight stays the same…
Thank you for this very helpful component!
Greetings Emanuel
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17
I have found the solution, you need to set the active attribute like this: [active]=“[arrayWithObjects[objectIndex]]”
In most cases this means you should refactor your data object to this format to get It working.
[active]=“value[0]” must be the array
I used the way that directly change its active value in ‘SelectComponent’, hope it works for you.
Thanks! In my case it was just [active]=“[object]”, because it was a single object!
I found this when trying to solve in reactive forms. Need to setValue like this:
this.select.active = [item.category];this.form.setValue({ name:this.item.name, description:this.item.description, category:[this.item.category.name], });this is assuming that you are using objects and not strings as items.
I’m having the same issue here. It looks like the right attribute should be [active] right now in stead of [initData], but I keep getting this error:
selectedItems.map is not a functionI had this problem, it was actually giving me errors for using “data” to populate the initial selected values in rc5, because there is an event called “data” which causes a conflict. So reading the source I did bump into [initData] and it worked.