How to fill select component programmatically


#1

Hi

This is my question, How can i fill select component programmatically? for example i have some JSON data and need to add OPTION child to this tag.

Thanks.


#2

You can use the AngularJS directive ng-repeat to go through your JSON data after you linked your data to a scope variable. If you use a service or something, the select can be updated automatically once it’s binded to the scope variable.

I made a small codepen to show you : http://codepen.io/akyh/pen/rBpAy?editors=101
When you click the upper button, it adds one element to my JSON data and it’s automatically refreshed in the view.

I hope that’s what you were looking for.


#3

Well done, it works, and how can set SELECTED attribute for one of them.


#4

Hi

I think that better is ng-options
https://docs.angularjs.org/api/ng/directive/select

maybe it’s harder to understand at the beginning but it’s more flexible and code is cleaner.

furthermore, select is not fine for mobile :wink: