I searched high and low for an explanation of how to remove an empty option. AngularJS always displays in select. I found a lot of information by doing this when the parameters are obtained from a JSON array located directly in the code, but I can not find anything about removing this empty option when working with data objects.
Say I have an object “foo” in my database, and “foo” has both a “name” and a “bar_id”.
Can someone give me a hint about this with data objects?
References: Angular JS Remove an empty option from a selection option
http://jsfiddle.net/MTfRD/3/ (This is someone else from the SO question linked above, the code is not mine, but taken from this fiddle).
JS:
function MyCtrl($scope) { $scope.typeOptions = [ { name: 'Feature', value: 'feature' }, { name: 'Bug', value: 'bug' }, { name: 'Enhancement', value: 'enhancement' } ]; $scope.form = {type : $scope.typeOptions[0].value}; }
I want to get foo.name and foo.bar_id. I want foo.name to be the option label and foo.bar_id the value (for example, <option value="foo.bar_id">foo.name</option> ). For each foo, foo.bar_id will become the identification parameter in the record to pull up and display "on change" (immediately after selecting the option).
I tried everything I could think of to set this up and nothing works. The worst part is that it either fails, leaving me without an indication of what I'm doing wrong, or complaining that foo is not an object, which disappoints me to the end. (I assure you, "foo" is loaded using an AJAX request in the actual code I'm working on, and works fine as an object everywhere - the NDA forbids me to use real code, though, sorry.)
Using the above example from another thread, I understand that in the select tag in the template I would assign something like ng-model="typeOptions" , but how would I get "typeOptions" to access foo and make foo.name the option label and foo.bar_id parameter value? Also, angular defaults for parameters (those that look like a generated index) are good for me, but I still need to call foo.bar_id to complete the task, so it should be "out there somewhere."
Any help would be greatly appreciated; I'm currently stuck with the hacker ng-repeat in the option itself, which, as I understand it, is not the best practice. Also, I'm still pretty new to Angular, and yet I find it somewhat confusing, so the simpler and clearer your answers are, the better I can use them successfully. Thanks!